Article Directory
Table of contents
- what is a statement
- Branch statement (selection construct)
- loop statement
- goto statement
1. What is a statement
Statements in C language can be divided into the following five categories:
- expression statement
int main()
{
3 + 5;
return 0;
}
- function call statement
#include <stdio.h>
int main()
{
printf("hehe");
return 0;
}
- control statement
- compound statement
- empty statement
int main()
{
;
}
Control statements are covered later in this chapter .
Control statements are used to control the execution flow of the program to realize various structural modes of the program (C language supports three structures : sequence structure , selection structure , and loop structure ), which are composed of specific statement definers. There are nine types in C language Control statements can be divided into the following three categories:
- Conditional judgment statements are also called branch statements: if statement, switch statement;
- Loop execution statement: do while statement, while statement, for statement;
- Turn statement: break statement, goto statement, continue statement, return statement.
2. Branch statement (select structure)
2.1 if statement
What is the grammatical structure of the if statement?
#include <stdio.h>
int main()
{
int age = 0;
//输入
scanf("%d", &age);
if (age < 18)
printf("未成年\n");
return 0;
}
#include <stdio.h>
int main()
{
int age = 0;
//输入
scanf("%d", &age);
if (age < 18)
printf("未成年\n");
else
printf("成年\n");
return 0;
}
#include <stdio.h>
int main()
{
int age = 0;
//输入
scanf("%d", &age);
//<18 青少年
//18~30 青年
//31~50 中年
//51~80 中老年
//81~100 老年
//101+ 老寿星
if (age < 18)
printf("青少年\n");
else if (age >= 18 && age <= 30)
printf("青年\n");
else if (age >= 31 && age <= 50)
printf("中年\n");
else if (age >= 51 && age <= 80)
printf("中老年\n");
else if (age >= 81 && age <= 100)
printf("老年\n");
else
printf("老寿星\n");
return 0;
}
Note: If the expression evaluates to true , the statement executes. (In C, 0 means false , non-zero means true .)
To execute multiple statements if a condition is true , you should use a code block . A pair of { }
here is a code block .
2.1.1 Floating else
When you write the following code:
#include <stdio.h>
int main()
{
int a = 0;
int b = 2;
if(a == 1)
if(b == 2)
printf("hehe\n");
else
printf("haha\n");
return 0;
}
What happens in the end?
The result is neither haha nor hehe, but prints nothing.
This is because the else matches the closest if .
Summarize:
- Appropriate use of {} can make the logic of the code clearer.
- Code style matters
2.1.2 Comparison of writing forms of if
//代码1
if (condition) {
return x;
}
return y;
//代码2
if(condition)
{
return x;
}
else
{
return y;
}
//代码3
int num = 1;
if(num == 5)
{
printf("hehe\n");
}
//代码4
int num = 1;
if(5 == num)
{
printf("hehe\n");
}
Code 2 and Code 4 are better, the logic is clearer, and it is less prone to errors.
2.1.3 Exercises
- Check if a number is odd
#include <stdio.h>
int main()
{
int n = 0;
//输入
scanf("%d", &n);
if (n % 2 == 1)
{
printf("是奇数\n");
}
else
{
printf("不是奇数\n");
}
return 0;
}
- Output odd numbers between 1-100
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 100)
{
//判断i是奇数的话,就打印i
if (i % 2 == 1)
printf("%d ", i);
++i;
}
return 0;
}
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 100)
{
printf("%d ", i);
i = i + 2;
}
return 0;
}
2.2 switch statement
The switch statement is also a branch statement, often used in multi-branch situations.
for example:
input 1, output monday
input 2, output tuesday
input 3, output wednesday
input 4, output thursday
input 5, output friday
input 6, output saturday
input 7, output sunday
What is the grammatical structure of the switch statement?
#include <stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
printf("星期1\n");
case 2:
printf("星期2\n");
case 3:
printf("星期3\n");
case 4:
printf("星期4\n");
case 5:
printf("星期5\n");
case 6:
printf("星期6\n");
case 7:
printf("星期天\n");
}
return 0;
}
2.2.1 break in switch statement
In the switch statement, we can't directly realize the branch (the above code is problematic) , and the real branch can be realized only by using it with break .
#include <stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
printf("星期1\n");
break;
case 2:
printf("星期2\n");
break;
case 3:
printf("星期3\n");
break;
case 4:
printf("星期4\n");
break;
case 5:
printf("星期5\n");
break;
case 6:
printf("星期6\n");
break;
case 7:
printf("星期天\n");
break;
}
return 0;
}
Sometimes our needs change:
- Input 1-5, the output is "weekday";
- Input 6-7, output "weekend"
#include <stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
}
return 0;
}
Summary: The actual effect of the break statement is to divide the statement list into different branch parts.
good programming habits
Add a break statement after the last case statement. (The reason for writing this way is to avoid forgetting to add a break statement after the last case statement before)
2.2.2 default clause
What if the value of the expression does not match the value of any case label?
In fact, nothing, the result is that all statements are skipped.
But what if you don't want to ignore the value of an expression that doesn't match all tags?
You can add a default clause in the statement list. When the value of the switch expression does not match the values of all the case labels, the statement after the default clause will be executed.
Therefore, only one default clause can appear in each switch statement , but it can appear anywhere in the statement list , and the statement flow will execute the default clause like a case label.
#include <stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
default:
printf("输入错误\n");
break;
}
return 0;
}
good programming habits
It is a good habit to put a default clause in each switch statement, and you can even add a break after it.
2.2.3 Exercises
#include <stdio.h>
int main()
{
int n = 1;
int m = 2;
switch (n)
{
case 1:
m++;
case 2:
n++;
case 3:
switch (n)
{
//switch允许嵌套使用
case 1:
n++;
case 2:
m++;
n++;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m = %d, n = %d\n", m, n);
return 0;
}
//m = 5, n = 3
3. Loop statement
3.1 while loop
What is the grammatical structure of the while statement?
The flow of while statement execution:
For example, we want to achieve:
Print the numbers 1-10 on the screen.
#include <stdio.h>
int main()
{
int n = 1;
while (n <= 10)
{
printf("%d ", n);
n++;
}
return 0;
}
3.1.1 break and continue in while statement
break introduction:
#include <stdio.h>
int main()
{
int n = 1;
while (n <= 10)
{
if (5 == n)
break;
printf("%d ", n);
n++;
}
return 0;
}
//1 2 3 4
Summary:
The function of break in the while loop:
In fact, as long as a break is encountered in the loop, all subsequent loops will be stopped and the loop will be terminated directly.
Therefore, the break in while is used to permanently terminate the loop.
continueIntroduction:
#include <stdio.h>
int main()
{
int n = 1;
while (n <= 10)
{
n++;
if (5 == n)
continue;//跳过本次循环continue后边的代码,直接去while循环的判断部分
printf("%d ", n);
}
return 0;
}
//2 3 4 6 7 8 9 10 11
Summary:
The function of continue in the while loop is:
continue is used to terminate this cycle, that is, the code after continue in this cycle will not be executed again, but directly jump to the judgment part of the while statement to judge the entry of the next cycle.
Look at a few more codes:
First introduce getchar and putchar:
#include <stdio.h>
int main()
{
int ch = getchar();//A
printf("%c\n", ch);//A
putchar(ch);//打印字符
return 0;
}
//代码1
#include <stdio.h>
int main()
{
int ch = 0;
//EOF - end of file 文件结束标志
//#define EOF (-1)
//在函数读取读取失败的时候返回了EOF
//scanf 函数读取成功,返回的是读取到的数据的个数
//读取失败返回EOF
//getchar 读取成功返回字符的ASCII码值
//读取失败返回EOF
//ctrl+Z - 会让scanf 或者 getchar 返回EOF
while ((ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}
Appropriate modification of the above code can be used to clean up the buffer:
#include <stdio.h>
int main()
{
//123456
char password[20] = {
0 };
printf("请输入密码:>");
scanf("%s", password);//123456 abc
printf("请确认密码(Y/N):");
int input = 0;
//scanf("%c", &input);//Y
//把\n处理掉
//getchar();
//清理掉缓冲区中剩余的数据
while (getchar() != '\n')
{
;
}
input = getchar();
if ('Y' == input)
printf("确认成功\n");
else
printf("确认失败\n");
return 0;
}
//代码2
#include <stdio.h>
int main()
{
char ch = '\0';
while ((ch = getchar()) != EOF)
{
if (ch < '0' || ch > '9')
continue;
putchar(ch);
}
return 0;
}
//这个代码的作用是:只打印数字字符,跳过其他的字符。
Attached:
Branch and Loop Statements (2)
Branch and Loop Statements (3)