Tour C language tutorial 5 - conditional statements
This is the fifth tutorial of traveling around C language. You will understand conditional statements in this article.
condition
The condition is to judge whether the statement is true. In C language, 0 means not true, which is false in logic, and non-0 means true, which is true in logic. For a conditional judgment statement, if the judgment is true, the value of the expression is 1, which is true. Otherwise, the value of the expression is 0, which is false. For example, if a = 2 and B = 3, the value of expression a > b is 0, because it is not true, and the value of a < B is 1, which is true.
Conditional statement
if and switch can be used to judge in C language
if
The structure of the if statement is as follows
if(Expression 1) { Execute code 1; } else if(Expression 2) { Execute code 2; } else if(Expression 3) { Execute code 3; } else if(...) { ...; } else { Execute code n; }
The execution logic of if is to first judge whether the expression in if is true. If it is true, execute code 1, and then skip other codes. If the expression in if does not hold, skip to the next else if() to judge whether the expression holds. If it holds, execute the corresponding code block. If it does not hold, execute the code in else all the time.
Since each code block in the if statement is parallel, which condition is satisfied will be executed. Once one condition is satisfied, other expressions will not be judged. If expression 1 is true, code block 2 will not be executed even if expression 2 is true again. When all expressions are not satisfied, the statements in else will be executed.
else if and else are not required
if(Expression 1) { Execute code 1; }
if(Expression 1) { Execute code 1; } else { Execute code 2; }
if(Expression 1) { Execute code 1; } else if(Expression 2) { Execute code 2; }
The above ways of writing are allowed.
#include <stdio.h> int main() { int a = 1; int b = 2; if (a == b) { printf("a be equal to b"); } else if (a > b) { printf("a greater than b"); } else { printf("a less than b"); } }
switch
The essence of switch jumps. His structure is as follows
switch(Integer variable) { case Constant 1: Code block 1; break; case Constant 2: Code block 2; break; default: Code block n break; }
This means that the value of switch will jump out of the program automatically, which means that the value of switch will jump out of the current code. If there is no value matching the integer in all case s listed, it will jump to the value in default.
Note that the switch must be an integer; case must be followed by a constant, i.e. a fixed value; default can be omitted. Because swich is essentially a jump, it will execute the code until it encounters a break;
switch(Integer variable) { case Constant 1: Code block 1; case Constant 2: Code block 2; default: Code block n break; }
For the above code, if the integer variable is equal to constant 1, it will jump to code block 1 and execute it down in sequence, because it does not encounter a break; He will also execute code block 2 and code block n.
#include <stdio.h> int main() { int a = 1; switch (a) { case 1: printf("1"); break; case 2: printf("2"); break; case 3: printf("3"); break; case 4: printf("4"); break; default: printf("Other figures"); break; } }
The difference between if and switch
swich can only be used to judge the value of a variable. It must be an integer. However, because its essence is jump, each statement is equal. It will not judge whether it is equal to constant 1 first and then constant 2. Even if there are enough case s, it will only judge once.
If can be applied to any expression. He will first calculate all the expressions in the if to judge whether the value of the expression is true. If it does not meet the requirements, he will judge the next if, that is, if the expression in the fifth if meets the conditions, the expressions in the first four if will also be calculated.
#include <stdio.h> int main() { int a = 4; if (a-- == 0) { printf("a = 0"); } else if(a-- == 0) { printf("a = 1"); } else if (a-- == 0) { printf("a = 2"); } else if (a-- == 0) { printf("a = 3"); } else if (a-- == 0) { printf("a = 4"); } }
Here we use the self subtraction we learned before. A – will first return its current value and then self subtraction. At the end of the run, you can see the output a = 4