C语言 流程控制

  • 决策

    决策结构要求程序员指定一个或多个要由程序评估或测试的条件,以及确定该条件为真的情况下要执行的一条或多条语句,以及指定该条件时要执行的其他语句(可选)确定为假。下面显示的是大多数编程语言中常见的典型决策结构的一般形式-
    c decision_making
    C编程语言将任何非零非null值假定为true,并且如果它为null,则将其假定为false值。
    C编程语言提供以下类型的决策语句。
    声明 描述
    if声明 一个if语句包含一个布尔表达式后跟一个或多个语句。
    if...else声明 一个if语句可以跟着一个可选的else语句,当布尔表达式是假的,其执行。
    嵌套if语句 您可以在另一个if or else if语句中使用一个if or else if语句。
    switch语句 switch语句允许一个变量来针对值的列表平等进行测试。
    嵌套的switch语句 您可以在另一个switch语句中使用一个switch语句。
    范例 - if声明
    condition1
    
    #include <stdio.h>
     
    int main () {
    
       /* local variable definition */
       int a = 10;
     
       /* check the boolean condition using if statement */
            
       if( a < 20 ) {
          /* if condition is true then print the following */
          printf("a is less than 20\n" );
       }
       
       printf("value of a is : %d\n", a);
     
       return 0;
    }
    
    尝试一下
    范例 - if...else声明
    condition1
    
    #include <stdio.h>
     
    int main () {
    
       /* local variable definition */
       int a = 100;
     
       /* check the boolean condition */
       if( a < 20 ) {
          /* if condition is true then print the following */
          printf("a is less than 20\n" );
       } else {
          /* if condition is false then print the following */
          printf("a is not less than 20\n" );
       }
       
       printf("value of a is : %d\n", a);
     
       return 0;
    }
    
    尝试一下
    范例 - 嵌套if语句
    
    #include <stdio.h>
     
    int main () {
    
       /* local variable definition */
       int a = 100;
       int b = 200;
     
       /* check the boolean condition */
       if( a == 100 ) {
       
          /* if condition is true then check the following */
          if( b == 200 ) {
             /* if condition is true then print the following */
             printf("Value of a is 100 and b is 200\n" );
          }
       }
       
       printf("Exact value of a is : %d\n", a );
       printf("Exact value of b is : %d\n", b );
     
       return 0;
    }
    
    尝试一下
    范例 - switch语句
    condition1
    以下规则适用于switch语句
    • switch语句中使用的表达式必须具有整数或枚举类型,或者是类类型,其中该类具有到整数或枚举类型的单个转换函数。
    • switch内可以有任意数量的case语句。每个case后面都跟要比较的值和一个冒号。
    • case的常量表达式必须与switch中的变量具有相同的数据类型,并且必须是常量或文字。
    • 当switch的变量等于case时,该case之后的语句将一直执行,直到到达break语句为止。
    • 当到达break声明,switch终止,并且控制流程跳转到以下switch语句中的下一行。
    • 并非每个case都需要break一下。直到达到switch最后的case如果没有switch的值出现控制流会落空。
    • 一个switch语句可以有一个可选的默认情况下,它必须出现在switch的最后。当所有情况都不为真时,可以使用默认情况来执行任务。在默认情况下,无需break。
    
    #include <stdio.h>
     
    int main () {
    
       /* local variable definition */
       char grade = 'B';
    
       switch(grade) {
          case 'A' :
             printf("Excellent!\n" );
             break;
          case 'B' :
          case 'C' :
             printf("Well done\n" );
             break;
          case 'D' :
             printf("You passed\n" );
             break;
          case 'F' :
             printf("Better try again\n" );
             break;
          default :
             printf("Invalid grade\n" );
       }
       
       printf("Your grade is  %c\n", grade );
     
       return 0;
    }
    
    尝试一下
    范例 - 嵌套的switch语句
    
    #include <stdio.h>
     
    int main () {
    
       /* local variable definition */
       int a = 100;
       int b = 200;
     
       switch(a) {
       
          case 100: 
             printf("This is part of outer switch\n", a );
          
             switch(b) {
                case 200:
                   printf("This is part of inner switch\n", a );
             }
       }
       
       printf("Exact value of a is : %d\n", a );
       printf("Exact value of b is : %d\n", b );
     
       return 0;
    }
    
    尝试一下
  • ?:运算符

    条件运算符?:可以替换上一章中替换if ... else语句。它具有以下一般形式-
    
    Exp1 ? Exp2 : Exp3;
    
    其中Exp1,Exp2和Exp3是表达式。注意冒号的使用和放置。表达式是这样确定的-
    • Exp1被测试。如果为true,则对Exp2进行求值并成为整个表达式的值。
    • 如果Exp1为false,则计算Exp3,其值成为表达式的值。