Easy to learn C language Chapter 4

Select structure programming

①Relational operators and relational expressions

②Logical operators and logical expressions

③if statement

⑥switch statement

 C language uses relational expressions and logical expressions to achieve double-branch selection through if statements, and use switch statements to achieve multi-branch selection.

4.1 Relational operators and relational expressions

"Relational operation" means "comparison operation, which compares two values, and the result of the comparison is to get two values, true and false.

Relational operators and their order of precedence C language provides 6 types of relational operators: <<= == >=> != Combination direction: from left to right priority level:

Relational expression A formula that connects two expressions with relational operators

The value of the relational expression: is the logical value "true" or "false", represented by 1 and 0

4.2 Logical operators and logical expressions            

A formula that connects relational expressions or logical quantities with logical operators is a logical expression.

Logical operators: C language provides 3 kinds of logical operators

Precedence of logical operators      

                   ! (Not), && (and), || (or)

Priority: High (2) (11) (12) Low Combination direction: from right to left, left to right, left to right

Note: In C language, the amount of calculation: ①0 means "false", calculation result: 0 means "false", ②non-zero means "true", 1 means

Points to note in logical operations:

Short-circuit characteristics: When solving a logical expression, not all logical operators are executed, but only when the next logical operator must be executed to find the solution of the expression, the operator is executed

Expression of complex logical conditions

Example: Determine whether a year is a leap year

判别闰年的条件(int year):
①能被4整除:
year%4==0
②能被4整除但不能被100整除:
(year%4==0)&&(year%100!=0)
③能被400整除:
year%400==0
综合起来:
((year%4==0)&&(year%100!=0))||year%400==0
优化语句:
(year%4==0&&year%100!=0)||year%400==0

4.3 if statement (conditional selection statement)

Three forms of If statement Form 1: Format: if (expression) statement

Form 2: Format:

if (expression)             

     Statement 1                

else             

     Statement 2

Form three:

format:

if (expression 1) statement 1                 

else if (expression 2) statement 2                  

else if (expression 3) statement 3           

……                 

else if (expression m) statement m                 

else statement n

Example: Determine the type of input characters

#include<stdio.h> 
void main()
 {  char c;
    printf("Enter a character:");
    c=getchar();
    if(c<32)  printf("The character is a control character\n");
    else if(c>='0'&&c<='9')  printf("The character is a digit\n");
    else if(c>='A'&&c<='Z')  printf("The character is a capital letter\n");
    else if(c>='a'&&c<='z')  printf("The character is a lower letter\n");
    else printf("The character is other character\n");
 }

Example: Input two real numbers and output two numbers in ascending order

 #include <stdio.h>
 void main()
 { 
    float a,b,t ;
    scanf("%f,%f",&a,&b);
    if(a>b)
       {t=a;a=b;b=t;}
    printf("%5.2f,%5.2f",a,b);
 }

Example: Input three numbers and output them in ascending order

 #include <stdio.h>
 void main()
 { float a,b,c,t ;
    scanf("%f,%f,%f",&a,&b,&c);
    if(a>b)
       {t=a;a=b;b=t;}
    if(a>c)
       {t=a;a=c;c=t;}
    if(b>c)
       {t=b;b=c;c=t;}
    printf(“%5.2f,%5.2f,%5.2f”,a,b,c);
 }

Nesting of If statements

Note: if ~ else pairing principle: by default {}, else is always paired with the unpaired if closest to it.

Conditional operator            

 In the if statement, when the expression is "true" and "false", only one assignment statement is executed to assign a value to the same variable, which can be processed with conditional operators.

 

Note: Conditional operator is the only ternary operator in C language

A few explanations of conditional operators:

①Conditional operators can be nested priority: 13

②Combination direction: from right to left

③Expression 1? Expression 2: Expression 3  

④ The type can be different, the expression value takes the higher type in expression 2 and expression 3

Example: Enter a letter, convert uppercase to lowercase, and then output the letter

 #include <stdio.h>
 void main()
 { char ch;
    scanf("%c",&ch);
    ch=(ch>=‘A’ && ch<=‘Z’)? (ch+32) : ch;
    printf(“%c“,ch);
 }

4.4 switch statement (multi-branch selection statement) General form:

A few instructions for switch

  • C1, C2,...Cn are constant expressions, and the values ​​must be different from each other
  • Constant expression serves as a statement label and must be used break
  • Multiple executable statements can be included after case, and {} is not necessary
  • switch can be nested
  • Multiple cases can share a set of execution statement examples  
  • switch nesting
  • #include <stdio.h>
     void main( )
          {   int x=1,y=0,a=0,b=0;
              switch(x)
              {    case  1:
                               switch(y)
                                {     case 0:   a++;  break;
                                       case 1:   b++;  break;
                                }
                    case  2:  a++;b++; break;
                    case  3:  a++;b++;
               }
               printf(“\na=%d,b=%d”,a,b);
           }
    

    Example: Determine whether a certain year is a leap year

③使用逻辑表达式
#include <stdio.h>
void main()                                        
{ int year,leap;
  scanf(“Enter year:%d”,&year);
  if((year%4==0&&year%100!=0)||(year%400==0)) 
      leap=1;
  else leap=0;
  if(leap) printf(“%d is”,year); 
  else printf(“%d is not”,year);
  printf(“a leap year.\n”);
}

The notes in this article are from the PPT of C program design by Tan Haoqiang, I love learning haha!

If you think the writing is good, please like it~

 

 

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/105822136