C language programming-choose structure programming

C language selection structure programming can be divided into two types of
single branch selection programming
single branch if statement format
if (expression)
statement 1;

//例子:(判断输出值)
 #include <stdio.h>
int main(){
    
    
 int a;
 int b;
 printf("请输入a,b,\n");
 scanf("%d,%d",&a,&b);
 if(a==b){
    
    
  printf("是的!a=b时正确的!\n");
  return 0;
 }
}

Multi-branch selection program design
Multi-branch selection structure has double-branch structure,
double-branch if statement format
if (expression) { statement 1; else statement 2; }




//例子:(判断两个数的最大值)
#include  <stdio.h>
int main(){
    
    
 int a;
 int b;
 printf("input a,b:" );
 scanf("%d,%d",&a,&b);
 if (a>b)
 {
    
    
  printf("max=%d\n",a );
 }
 else{
    
    
       printf("max=%d\n",b );
 }
 return 0;
}

Multi-branch structure
1.
if (expression) { statement 1; else if () { statement 2; } } 2. else if statement if (expression) { statement else if (/* expression /){/ statement /} else if (/ expression /){/ statement*/} } else{statement}












//判断数字值代表的星期

#include <stdio.h>
 int main(int argc, char const *argv[])
 {
    
    
  int a;
  printf("please input your  number" );
  scanf("%d",&a);
  if (a==1)
   {
    
    
     printf("Today is monday!\n");
   }
   
   else if (a==2)
   {
    
    
    printf("Today is tuerday\n" );
   }
   
   else if (a==3)
   {
    
    
    printf("Today is wednesday\n" );
   }
   
   else if (a==4)
   {
    
    
    printf("Today is thursday\n" );
   }
   else if (a==5)
   {
    
    
    printf("Today is firday\n" );
   }
   else if (a==6)
   {
    
    
    printf("Today is saturday\n" );
   }
    else if (a==7)
   {
    
    
    printf("Today is sunday\n" );
   }
       else{
    
    
             printf("sorry ! your enter number is false! please input again! thanks\n" );
   }
  printf("It is good week!\n" );
  return 0;
 }
例子(判断两个数值定义)
#include <stdio.h>
int main(){
    
    
 char sex;                    /定义字符型数值sex/
 int tall;                    /定义整型数值tall/
 printf("请输入sex、tall数值");
 scanf("%c%d",&sex,&tall);    /输入sex tall的数值/
 if(sex='F'){
    
                     /判断输入sex的值是否等于F/
  if(tall>=150){
    
               /判断tall是否大于等于150/
   printf("A\n");       /当tall>=150时输出A/
   
   }
   else{
    
                    /当tall小于150时输出B/
   printf("B\n");
   }
   }
  else{
    
                           /判断sex不等于F执行/
  if(tall>=170){
    
              /判断tall值是否>=170/
   printf("A\n");      /当tall>=170时输出A/
  }
  else{
    
    
  printf("B\n");          /当tall小于170时输出B/
  }
  }
  return 0;
 }

if else matching principle
else is the closest to it at the same level, and there is no if statement matching with other else statements
switch (switch branch statement)
switch (expression) { case 1: statement group; break;


case 2:
statement group;
break;

case 3:
statement group;
break;

case n:
statement group;
break;

default;
statement group;
}

/*例子输入成绩查询等级*/
#include <stdio.h>
int main(){
    
    
 int score;
 int max;
 printf("please input score");
 scanf("%d",&score);
 max =  0<score&&score<100 ? score/10:-1;
 switch(max){
    
    
  case 10:
  printf("grade =A\n");
  break;

case 9:
  printf("grade =A\n");
  break;

case 8:
  printf("grade =B!\n");
  break;

case 7:
  printf("grade =C\n");
  break;
case 6:
  printf("grade =D/n");
  break;


  case 5:
  case 4:
  case 3:
  case 2:
  case 1:
  case 0:
  printf("sorry ! please  study hard !/n");
  break;
  default:
  printf("Input error again!/n" );
  return 0;
   }
}	

Conditional operator
example
if(a>b){ max=a; } else{ max=b } Let’s look at this example. When judging the values ​​of a and b, is it a bit cumbersome to use branch statements, but we can use Conditional operators to simplify these things can be viewed as: a>b?a:b This means that when a>b is true, then a is the value of a>b, if it is false, then b is a> the value of b







Guess you like

Origin blog.csdn.net/weixin_45743004/article/details/103848709