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]
  • 相关阅读:
    C# Distinct去重泛型List
    分布式高并发系统如何保证对外接口的幂等性?转载
    描述文件安装失败:Please ensure the provisioning profile is configured for this device. If not, please try to generate
    Java基础-概述
    Java基础-语法
    使用 Proxifier + Charles 抓取本机 Http请求 /
    Git回退服务器版本及receive.denyDeleteCurrent配置
    codeforces#1549D. Integers Have Friends
    NOI 2021 ~Last Celebration~
    docker
  • 原文地址:https://www.cnblogs.com/LoveFishC/p/3845951.html
Copyright © 2011-2022 走看看