Select the type of statement
There are two kinds of selection statements in c language
1. if else statement
2. switch statement
As the name suggests, we will face some choices. If the selection is correct, it will be executed, otherwise it will not be executed
c language is a structured programming language
There are sequential structure, selective structure and cyclic structure
The selection statement is the selection structure
Meaning of statement
What is a statement?
A semicolon in c language; Separated is a statement
For example:
1|3+5; 2|;//No content is an empty statement
True and false judgment
How does c language express true and false?
Non 0 is true, 0 is false
What are statement items
//Some case statements case Integer constant expression: sentence; //switch allows nested use, that is, multiple case statements can be used
###Syntax structure of if statement
Writing method 1:
if(expression)//If the expression result is true, the statement will be executed, otherwise it will not be executed sentence;
Code example:
#include <stdio.h> int main() { int age = 10; if (age >= 18)//The result is false and the statement is not executed printf("adult"); return 0; }//So the print result is empty
Method 2:
if(expression)//If the expression is true, statement 1 is executed, otherwise statement 1 is executed Statement 1; else Statement 2;
Code example:
#include <stdio.h> int main() { int age = 10; if (age >= 18)//Statement is false, execute statement 2 printf("adult"); else printf("under age");//Statement 2 is a minor, so the printed result is a minor return 0; }
Writing method 3:
//Multi branch if(Expression 1)//If expression 1 is true, statement 1 will execute; otherwise, if expression 2 is true, statement 2 will execute; otherwise, statement 3 will execute Statement 1; else if(Expression 2) Statement 2; else Statement 3;
Code example:
#include <stdio.h> int main() { int age = 20; if (age >18)//Expression 1 is true printf("adult\n");//The print result is adult else if(age = 18) printf("Just grown up"); else printf("under age\n"); return 0; }
Common problems in if statements
Output does not match imagination, {} usage
for example
#include <stdio.h> int main() { int age = 20; if (age >= 18) printf("adult\n"); else printf("under age\n"); printf("Can't fall in love"); return 0; }//The printed result is adult and can't fall in love
Why does this happen?
Because each if and else can only control one statement
If we want else to control multiple statements
At this time, we will use {} to put multiple statements together
#include <stdio.h> int main() { int age = 20; if (age >= 18) printf("adult\n"); else { printf("under age\n"); printf("Can't fall in love");//Use braces to put the two statements together } return 0; }//Printed out as adult, the result is correct
Judgment condition error, use of logical operator
for example
#include <stdio.h> int main() { int age = 40; if (age <18) printf("juvenile\n"); else if(18<=age<=25)//When the judgment enters this expression, the result is true when 18 < = 40, and 1,1 < = 30 is also true printf("youth\n"); else printf("other"); return 0; }//So the result is youth
If we want to get the right result
At this time & & is needed to connect multiple judgments
#include <stdio.h> int main() { int age = 40; if (age <18) printf("juvenile\n"); else if(18<=age&&age<=25)//Use & & to connect multiple judgments printf("youth\n"); else printf("other"); return 0; }//The printed results are other, and the results are correct
Syntax structure of switch statement
switch(Integer expression)//Integer expressions can also be characters because the ASCII value of a character is an integer { Statement item; }
Code example
#include <stdio.h> int main() { int day = 1; switch (day)//Parentheses cannot be omitted { case 1: printf("Monday"); } return 0; }//The printed result is Monday
Common errors in switch statements
Statements after multiple case s are printed consecutively (the role of break in switch)
for example
#include <stdio.h> int main() { int day = 0; scanf("%d", &day); switch (day) { case 1: printf("Monday\n"); case 2: printf("Tuesday\n"); case 3: printf("Wednesday\n"); case 4: printf("Thursday\n"); case 5: printf("Friday\n"); case 6: printf("Saturday\n"); case 7: printf("Sunday\n"); } return 0; }//When we enter 6, we print Saturday and Sunday
This is because if we do not stop the sixth instruction after it is executed, it will continue to execute until the statement is executed. If there is a default expression, the statement after default will also be executed
#include <stdio.h> int main() { int day = 0; scanf("%d", &day); switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; } return 0; }//When we add break and enter 6, the printed result is Saturday, and break prevents the program from continuing
**Note: * * case: it is not necessary to add break later, just add it as required
Usage of default in switch statement
When we enter a value, there is always no value in the case statement, when we want to give people a feedback
At this time, we can use default
for example
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int day = 0; scanf("%d", &day); switch (day) { case 1: case 2: case 3: case 4: case 5: printf("weekdays\n"); break; case 6: case 7: printf("Rest Day\n"); break; default: printf("Input error"); break;//When we print 8, 9 and 10, the printed result is an input error } return 0; }
**Note: only one default statement can appear in each switch statement, but it can appear anywhere in the statement list (switch {}) * *