Judgement in C language (if statement\if...else statement\switch statement\operator (ternary operator)\ (can understand with 0 basics)

Summary of all judgments in C language (judging adult and minor as an example)


The judgment structure requires the programmer to specify one or more conditions to be evaluated or tested, as well as the statement to be executed when the condition is true (required) and the statement to be executed when the condition is false (optional).

The C language assumes any non-zero and non-empty values ​​as true, and zero or null as false.


1, if statement

An if statement consists of a Boolean expression followed by one or more statements.


#include <stdio.h>
int main ()
{
    
    
   int age=0;
   scanf("%d",&age);
   	if( age >18)
   		{
    
    
      		 printf("成年" );
  		}
  	if( age <18)
  	    {
    
    
   			printf("未成年");
	   	}
   return 0;
}

Insert picture description here


Insert picture description here


*******************************************************************************************************

2. If...else statement
An if statement can be followed by an optional else statement. The else statement is executed when the Boolean expression is false.


#include <stdio.h>
int main ()
{
    
    
   int age=0;
   scanf("%d",&age);
   	if( age >=18)
   		{
    
    
      		 printf("成年" );
  		}
	else
  	    {
    
    
   			printf("未成年");
	   	}
   return 0;
}

Insert picture description here


Insert picture description here


*******************************************************************************************************

3, switch statement
A switch statement allows to test the situation when a variable is equal to multiple values.


#include <stdio.h>
int main (void)
{
    
    
   int age=0;
    printf("请输入你要查询的年龄段" );
   scanf("%d",&age);
   switch (age){
    
    
   case 10: 
      printf("未成年\n" );
      break;
   case 20: 
      printf("成年\n" );
      break;
   case 40 :
      printf("中年\n" );
      break;
   case 80: 
      printf("老年\n" );
      break;
   default :
   	printf("未找到你要查询的年龄段\n" );
   	break;
   }
   return 0;
}

Insert picture description here


Insert picture description here


**************************************************************************************************************************************

4. Operators (ternary operators)
conditional operators can be used to replace if...else statements.


#include<stdio.h>
int main(void)
{
    
    
    int age;
    printf("请输入你要查询的年龄段:");
    scanf("%d",&age);
    (age>18)?printf("成年"):printf("未成年");
}
  

Insert picture description here


Insert picture description here
If there is something wrong,
please correct me.
Leave your footsteps below.
(﹡ˆOˆ﹡)
**

Guess you like

Origin blog.csdn.net/qq_51932922/article/details/114036794