Loops and branches (knowledge summary)
- 1. if statement
- 2. switch statement
- 3. for loop
- 4. while loop
- 5. do...while loop
1. if statement
1. Single branch if statement
if (expression)
statement;
(1)Code example
if(a==1)
printf("梦");
(2)Execution process
Semantics: The expression is evaluated first, and if the expression is "true", the statement is executed; otherwise, the statement is not executed, and the next statement of the if statement is executed directly .
(3)Attention
There can only be one statement in a single-branch if . If there are multiple statements, they should be enclosed in curly braces .
Sample code:
#include<stdio.h>
int main()
{
int a=0;
int b=1;
if(a==1)
if(b==1)
printf("梦");
else
printf("渊");
return 0;
}
The running result is empty:
Explanation : The following code is generally a single-branch statement (the end mark of the statement is " ; "):
if(a==1)
if(b==1)
printf("梦");
However, in the sample code, else is paired with if(b==1) to form an if-else statement ; so the if(a==1) statement is the following code:
if(b==1)
printf("梦");
else
printf("渊");
So the example code is equivalent to:
#include<stdio.h>
int main()
{
int a=0;
int b=1;
if(a==1)
{
if(b==1)
printf("梦");
else
printf("渊");
}
return 0;
}
Combined with the above flow chart, we know: when if(a==1) is judged to be false , its statement is not executed, and the next statement is executed directly , that is, return 0;
after the code is executed, nothing is printed, so the running result is empty
2. Double-branch if statement (if-else statement)
if (expression)
statement 1;
else
statement 2;
(1)Code example
if(a==1)
printf("梦");
else
printf("渊");
(2)Execution process
3.Multiple branches
if (expression)
statement 1;
else if (expression 2)
statement 2;
else if (expression 3)
statement 3;
...
else
statement n;
(1)Code example
if(a==1)
printf("梦");
else if(a==2)
printf("渊");
else if(a==3)
printf("wo");
else
printf("ya");
(2)Execution process
(3)Use
Question:
The code to implement the following segmented function is as follows :
#include<stdio.h>
int main()
{
int x = 0;
scanf("%d", &x);
if (x >= 2)
printf("y=3\n");
else if (x > 0)
printf("y=%f\n", -1.0 / 2 * x);
else if (x >= -1)
printf("y=%d\n",2*x+2);
return 0;
}
When writing else if (x > 0), many people will write else if (x > 0&&x < 2) , but this is unnecessary, because it is only when
if (x >=2) is false, that is, when x<2 Else if (x > 0) will be executed , and the same applies later.
3. Nesting of if statements
(1) Leave empty else
else is always paired with the nearest unmatched if regardless of the arrangement of the code
Look at the following piece of code:
#include<stdio.h>
int main()
{
int a=0;
int b=1;
if(a==0)
if(b==0)
printf("梦");
else
printf("渊");
return 0;
}
The operation result is abyss , not dream , because else is paired with if(b==0)
(2) Multiple if-else statements
Use curly braces {} to separate them
Code example:
#include<stdio.h>
int main()
{
int a=0;
int b=1;
if(a==1)
{
if(b==1)
printf("梦");
else
printf("ya");
}
else
printf("渊");
return 0;
}
4. Normative issues
When there are multiple statements, it is better to enclose them in curly braces " {} ".
For example:
if(a==1)
{
printf("梦");
printf("入");
}
else if(a==2)
{
printf("渊");
printf("底");
}
else if(a==3)
printf("wo");
else
printf("ya");
5. Other issues that need attention
(1) == and =
Look at the following piece of code:
#include<stdio.h>
int main()
{
int a=0;
if(a=1)
printf("梦");
else
printf("渊");
}
The operation result is a dream , not an abyss
. Because a=1 means that 1 is assigned to a, which causes the expression to always be true , and what is printed is a dream , not an abyss
. To print an abyss , you should change = to ==
==.judgment, and = meansAssignment, don’t write it wrong.
2. switch statement
switch(expression)
{
case constant expression 1: statement 1; break;
case constant expression 1: statement 1; break;
...
case constant expression n: statement n; break;
default: statement n+1; break;
}
1. Code examples
(1)
#include<stdio.h>
int main()
{
int day=0;
scanf("%d",&day);
switch(day)
{
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;
default://默认值 可加可不加
printf("输入错误\n");
break;//这里的break可加可不加,但最好加上
}
return 0;
}
(2)
#include<stdio.h>
int main()
{
int month=0;
printf("输入月份:");
scanf("%d",&month);//int a=(month-1)/3;
switch((month-1)/3)//switch(a)
{
case 0:
printf("%d月是第1季度\n",month);
break;
case 1:
printf("%d月是第1季度\n",month);
break;
case 2:
printf("%d月是第1季度\n",month);
break;
case 3:
printf("%d月是第1季度\n",month);
break;
default:
printf("输入错误\n");
break;
}
return 0;
}
2. Description
(1) The expression after switch can be any expression , as shown in the example, but its value can only beInteger type, character type, enumeration type; If the expression is too long, you can define another variable to represent the expression, see example 2. Notes
(2) The value of the constant expression after each case should be different from each other , otherwise it will conflict with each other
(3) Each case and default The order of appearance does not affect the execution result
(4) Multiple cases can share a set of execution statements
(5) The switch statement allows nested use
The code example of (4) (modified from Example 2) is as follows:
#include<stdio.h>
int main()
{
int day=0;
scanf("%d",&day);
switch(day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("工作日\n");
break;
case 6:
case 7:
printf("休息日\n");
break;
default://默认值 可加可不加
printf("输入错误\n");
break;//这里的break可加可不加,但最好加上
}
return 0;
}
3. for loop
for(expression1;expression2;expression3)Note the semicolon
{ Loop body statement;
Curly braces can be omitted when not needed
}
1. Execution process
Semantics:
2. Code examples
#include<stdio.h>
int main()
{
//也可写成下面这样,但是不建议这样写
int i,sum; //int sum;
sum=0; //sum=0;
for(i=1;i<4;i++) //for(int i=1;i<4;i++)
{
sum=sum+i;
printf("%d\n",sum);
}
return 0;
}
3. Omission of the general form of the for loop statement (combined with the above code example)
(1) Omit expression 1
for(i=1;i<4;i++)
sum=sum+i;
Equivalent to
i=1
for(;i<4;i++)
sum=sum+i;
(2) Omit expression 2
for(i=1;;i++)
When expression 2 is omitted, it means that the loop is not controlled . If there is no other processing, an infinite loop will be formed.
(3) Omit expression 3
for(i=1;i<4;i++)
sum=sum+i;
Equivalent to
for(i=1;i<4;)
sum=sum+i;
i++;
(4) Omit the loop body statement (expression 3 is a comma expression)
for(i=1;i<4;i++)
sum=sum+i;
Equivalent to
for(i=1;i<4;sum=sum+i,i++) //后面用逗号隔开
(5) Expression 1 is a comma expression
sum=0;
for(i=1;i<4;i++)
sum=sum+i;
Equivalent to
for(sum=0,i=1;i<4;i++)
sum=sum+i;
illustrate:
The above introduction shows that the for loop control structure is powerful, but it is not recommended to write code like this.
4. while loop
while(expression)
{ loop body;
Curly braces can be omitted when not needed
}
1. Execution process
2. Code examples
#include<stdio.h>
int main()
{
int i,sum=0;
while(i<4)
{
sum=sum+i;
printf("%d\n",sum);
i++;
}
return 0;
}
5. do…while loop
do
{ loop body }while(expression);
Don’t miss the semicolon
1. Execution process
2. Code examples
#include<stdio.h>
int main()
{
int i,sum=0;
do
{
sum=sum+i;
printf("%d\n",sum);
i++;
}while(i<4);
return 0;
}