zoukankan      html  css  js  c++  java
  • 分支程序设计02 零基础入门学习C语言11

    第四章:分支程序设计02

    让编程改变世界

    Change the world by program


     

    if语句

      用if语句可以构成分支结构。它根据给定的条件进行判断,以决定执行某个分支程序段。C语言的if语句有三种基本形式。

    第一种形式为基本形式:

    if(表达式)

    语句

    其语义是:如果表达式的值为真,则执行其后的语句,否则不执行该语句。其过程可表示为下图。 [caption id="attachment_69" align="aligncenter" width="150"] if语句三种形式[/caption] [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        int a,b,max;
    
        printf("n input two numbers:   ");
        scanf("%d%d",&a,&b);
        max=a;
        if(max < b) 
            max = b;
        printf("max=%d",max);
    }
    [/codesyntax]  

    第二种形式为:

    if(表达式)

    语句1;

    else

    语句2;

    [caption id="attachment_70" align="aligncenter" width="150"] if语句三种格式[/caption] [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        int a, b;
    
        printf("input two numbers:     ");
        scanf("%d%d",&a,&b);
        if( a > b )
             printf("max=%dn",a);
        else
             printf("max=%dn",b);
    }
    [/codesyntax]  

    第三种形式为 if-else-if 形式:

      前二种形式的if语句一般都用于两个分支的情况。当有多个分支选择时,可采用if-else-if语句,其一般形式为: [caption id="attachment_71" align="aligncenter" width="150"] if语句三种格式[/caption] if语句的三种形式 [caption id="attachment_72" align="aligncenter" width="150"] if语句三种格式[/caption] [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        char c;
    
        printf("input a character:    ");
        c = getchar();
    
        if( c < 32 )		
            printf("This is a control charactern");
        else if( c>='0' && c<='9' )
            printf("This is a digitn");
        else if( c>='A' && c<='Z' )
            printf("This is a capital lettern");
        else if( c>='a' && c<='z' ) 
            printf("This is a small lettern");
        else     
            printf("This is an other charactern");
    }
    [/codesyntax] [buy] 获得所有教学视频、课件、源代码等资源打包 [/buy] [Downlink href='http://kuai.xunlei.com/d/LEIPICEQUCAR']视频下载[/Downlink]
  • 相关阅读:
    Avizo
    NEWS
    HOWTO
    InstallShield 2012 Spring优惠升级到最新版本(2015.4.30之前)
    Windows系统补丁KB2962872导致InstallShield无法启动(解决方案已更新)
    HOWTO: InstallScript MSI工程取Log
    安装软件列表
    阿里云推荐码 hut29f
    ios 缺少合规证明
    ios开发错误之: Undefined symbols for architecture x86_64
  • 原文地址:https://www.cnblogs.com/LoveFishC/p/3845951.html
Copyright © 2011-2022 走看看