C language branch statement
1. What is a statement
Sentence: In C language, there is one; separated by one sentence.
E.g:
int a = 10;
printf("haha\n");
10;
'A';
; //空语句,啥都不执行
2. Branch statement (choose structure)
2.1 if statement & precautions
2.1.1 Grammatical structure
(1)
if (表达式)
{
代码块;
}
(2)
//二选一
if (表达式)
{
代码块1;//表达式为真
}
else
{
代码块2;//表达式为假
}
(3)
//多分支 多选一
if (表达式1)
{
代码块1;
}
else if (表达式2)
{
代码块2;
}
else
{
代码块3;
}
Explanation:
1. If statement is a selective branch statement, as long as one condition is met, other statements will not be executed.
2. After the if () judges the result returned by the expression in (). In C language, 0 is false and non-zero is true.
3. The condition is judged to prevent mistaken writing as if (flag=6), which can be written as if (6==flag).
3. A {} is a code block, try to use {} to make the logic clearer, to show that it is a complete code block and not easy to make mistakes. Otherwise, it may happen that the compilation fails or the output result is wrong. Such as:
(1) The compilation fails
//error, the compilation fails
int main()
{
if (a > 10)
i++;
j++;//error,编译不通过
else
j++;
return 0;
}
//Recommended writing, with all {}
int main()
{
if (a > 10)
{
i++;
j++
}
else
{
j++;
}
return 0;
}
(2) The problem of dangling else
// Not adding all {}, there is dangling else
int main()
{
int a = 0;
int b = 2;
if (1 == a)
if (2 == b)
printf("hehe\n");
else
printf("haha\n");
return 0;
}
Output:
//Recommended writing, add all {}
int main()
{
int a = 0;
int b = 2;
if (1 == a)
{
if (2 == b)
{
printf("hehe\n");
}
}
else
{
printf("haha\n");
}
return 0;
}
Output: 4.
Example of multi-branch if statement:
int main()
{
int age = 0;
scanf("%d", &age);
if (age<18)
{
printf("少年\n");
}
else if (age >= 18 && age<30)
{
printf("青年\n");
}
else if (age >= 30 && age<50)
{
printf("中年\n");
}
else if (age >= 50 && age<80)
{
printf("老年\n");
}
else
{
printf("长命百岁\n");
}
system("pause");
return 0;
}
2.1.2 Dangling else
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
if(a == 1)
if(b == 2)
printf("hehe\n");
else//悬空else
printf("haha\n");
return 0;
}
输出:hehe
Note:
1. The else here is the dangling else.
2. When the if else statement is nested, else adopts the nearest principle to match the nearest if.
3. When writing code, try to bring {} to avoid the occurrence of dangling else, which may cause the output result to be wrong. For an example, see 2.1.1 (2) dangling else problem.
2.2 switch statement & matters needing attention
2.2.1 Grammatical structure
//switch语句是C语言中经典多分支语句
switch (整型表达式)
{
case 常量表达式1:
语句;
case 常量表达式2:
语句;
...
case 常量表达式n:
default:
语句;
}
Explanation:
1. The switch statement is a matching jump, the case only has a judgment function, no branching function; where the
case matches, jump to which position, starting from the matched position and then executed; the
case statement can be branched after the break is added.
1. Integer expressions include int, char, long long, short types, int a=10; a>10; (1+2); expressions, etc.
2. Constant expressions such as 97, '7' and so on.
2.2.2 break in switch statement
Role: The break statement in the switch statement can achieve branching.
Example:
(1) No break
switch (3)
{
case 1:
printf("星期一\n");
case 2:
printf("星期二\n");
case 3:
printf("星期三\n");
case 4:
printf("星期四\n");
case 5:
printf("星期五\n");
case 6:
printf("星期六\n");
case 7:
printf("星期天\n");
}
Output:
(2) There is a break
switch (3)
{
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
case 7:
printf("星期天\n");
break;
}
Output:
(3) Demand changes
int main()
{
int day = 3;
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekday\n");
break;
}
}
Output:
2.2.3 default statement
Function: When the value of the expression does not match the case, the default statement is executed.
Explanation:
1. The default statement can appear in any place where a case statement appears.
2. When the value of the expression after switch does not match the case, the default statement is executed, and only one default can appear in a switch statement.
3. The default statement can be omitted, but as a good programming practice, you can add default to the case statement and add break after it.
About the default position example:
(1) at the end
int main()
{
switch ('r')
{
case 'w':
printf("工作日\n");
break;
case 'r':
printf("休息日\n");
break;
case's':
printf("加班日\n");
default:
printf("输入有误\n");
break;
}
return 0;
}
Output:
(2) in the middle
// before the matching sentence
int main()
{
switch ('r')
{
case 'w':
printf("工作日\n");
break;
default:
printf("输入有误\n");
break;
case 'r':
printf("休息日\n");
break;
case's':
printf("加班日\n");
}
return 0;
}
Output:
//After the matching statement
int main()
{
switch ('r')
{
case 'w':
printf("工作日\n");
break;
case 'r':
printf("休息日\n");
break;
default:
printf("输入有误\n");
break;
case's':
printf("加班日\n");
}
return 0;
}
Output:
//No match in the case statement
int main()
{
switch ('N')
{
case 'w':
printf("工作日\n");
break;
default:
printf("输入有误\n");
break;
case 'r':
printf("休息日\n");
break;
case's':
printf("加班日\n");
}
return 0;
}
Output:
2.2.4 Nesting of switch statements
Example: (You can try to output the result)
int main()
{
int n = 1;
int m = 2;
switch (n)
{
case 1:
m++;
case 2:
n++;
case 3:
switch (n)
{
case 1:
n++;
case 2:
m++;
n++;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m = %d, n = %d\n", m, n);
return 0;
}
Output:
Description:
1. The switch statement supports nested use.
2. When the current set is used, the break in the switch statement just jumps out of the nearest switch statement.
3. Code block
1. A {} is a code block.
2. Any variable defined in a code block is called a temporary variable, and a function is also a code block, because functions are also enclosed in {}.
3. All variables defined in the code are called local variables, which are only valid in this code block.
(1) error
int main()
{
int flag = 6;
if (6 == flag)
{
int x = 10;
}
printf("%d\n", x);
return 0;
}
Output:
(2) Compile passed
int main()
{
int flag = 6;
if (6 == flag)
{
int x = 10;
printf("%d\n", x);
}
int x = 20;
printf("%d\n", x);
return 0;
}
Output:
Finally,
thank you for watching~~
Please correct me if there is any problem with the content of the article!
Come on together!