Branch statement and loop statement in C language (2)

Branch statement and loop statement in C language (2)


[Preface] This blog post is a supplement to the content of branch statements and loop statements (1) in C language, but this part is the focus of C language, so friends, take a good look.

Before entering the text, I would like to trouble you to learn the branch statement and loop statement in C language (1).
link: link

1. Loop statement

  • while
  • for
  • do while
    is like:

1. while loop

We've got that, the if statement:

if (条件)
	语句;

When the condition is met, the statement after the if statement is executed, otherwise it is not executed.
But this statement will only be executed once.
Because we find that many practical examples in life are: we need to do the same thing many times.
So how do we do it?
The C language introduces us: while statement, which can realize loop.
While syntax structure:

while (表达式)
	循环语句;

The flow of while statement execution:

For example, we realize:
print the numbers 1-10 on the screen.
insert image description here
The above code has helped me understand the basic syntax of the while statement, so let's understand it again: 1.1 Introduction to break and continue break
in the while statement Code example

#include <stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		if (i == 5)
			break;
		printf("%d ", i);
		i = i + 1;
	}
	return 0;
}


insert image description here
Summary of the results of the code output here :
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.
So: the break in while is used to permanently terminate the loop.
continueIntroduction
Code Example 1

#include <stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		if (i == 5)
			continue;
		printf("%d ", i);
		i = i + 1;
	}
	return 0;
}

The result of the code output here Summary
insert image description here
of the results of the code output here
insert image description here
:
The function of continue in the while loop is:
continue is used to terminate this loop, that is, the code after continue in this loop will not be executed again,
but directly jump Go to the judgment part of the while statement. Carry out the entry judgment of the next cycle.

2. for loop

We already know about the while loop, but why do we need a for loop?
First look at the syntax of the for loop:
2.1 for syntax

for (表达式1; 表达式2; 表达式3)
	循环语句;

Expression 1
Expression 1 is the initialization part , which is used to initialize the loop variable.
Expression 2
Expression 2 is the condition judgment part , which is used to judge the termination of the loop.
Expression 3
Expression 3 is an adjustment part , which is used to adjust the loop condition.

The actual problem:
print the numbers 1-10 on the screen using a for loop.
insert image description here
The execution flow chart of the for loop:

Now let's compare the for loop and the while loop.

int i = 0;
//实现相同的功能,使用while
i = 1;//初始化部分
while (i <= 10)//判断部分
{
    
    
	printf("hehe\n");
	i = i + 1;//调整部分
}
//实现相同的功能,使用while
for (i = 1; i <= 10; i++)
{
    
    
	printf("hehe\n");
}

It can be found that there are still three necessary conditions for the loop in the while loop, but due to the style problem, the three parts may deviate far away
, so
the search and modification are not concentrated and convenient enough. Therefore, the style of the for loop is better; the frequency of the for loop is also the highest.
2.2 break and continue in the for loop
We found that break and continue can also appear in the for loop, and their meaning is the same as in the while loop.
But there are still some differences:
Code 1
insert image description here
Code 2
insert image description here
2.3 The loop control variable of the for statement
Suggestions:

  1. Do not modify the loop variable in the body of the for loop to prevent the for loop from getting out of control.
  2. It is recommended that the value of the loop control variable of the for statement be written in the way of "open interval after closing first".
int i = 0;
//前闭后开的写法
for (i = 0; i < 10; i++)
{
    
    
}
//两边都是闭区间
for (i = 0; i <= 9; i++)
{
    
    
}

3. do...while () loop

3.1 The syntax of the do statement:

do
	循环语句;
while (表达式);

3.2 Execution process

3.3 The characteristics of the do statement
The loop is executed at least once, and the usage scenarios are limited, so it is not often used.

#include <stdio.h>
int main()
{
    
    
	int i = 10;
	do
	{
    
    
		printf("%d\n", i);
	} while (i < 10);
	return 0;
}


3.4 break and continue code in do while loop 1
insert image description here

Code 2
insert image description here
can be seen:
the break and continue in the do while loop are exactly the same as in the while loop.

Two, go to statement

The C language provides goto statements that can be abused at will and labels that mark jumps.
Theoretically, the goto statement is not necessary, and in practice, the code can be easily written without the goto statement.
However, the goto statement is still useful in some occasions. The most common usage is to terminate the processing of the program in some deeply nested structures
.
For example: Jump out of two or more layers of loops at a time.
The use of break in this case of multi-layer loop cannot achieve the purpose. It can only exit from the innermost loop to the previous loop.
The scenarios where the goto language is really suitable are as follows:

for (...)
for (...)
{
    
    
	for (...)
	{
    
    
		if (disaster)
			goto error;
	}
}
…
error :
if (disaster)
// 处理错误情况

The following is an example of using the goto statement, and then replacing the goto statement with a loop implementation:
a shutdown program

#include <stdio.h>
int main()
{
    
    
	char input[10] = {
    
     0 };
	system("shutdown -s -t 60");
again:
	printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
	scanf("%s", input);
	if (0 == strcmp(input, "我是猪"))
	{
    
    
		system("shutdown -a");
	}
	else
	{
    
    
		goto again;
	}
	return 0;
}

And if you don't use the goto statement, you can use a loop:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    
    
	char input[10] = {
    
     0 };
	system("shutdown -s -t 60");
	while (1)
	{
    
    
		printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
		scanf("%s", input);
		if (0 == strcmp(input, "我是猪"))
		{
    
    
			system("shutdown -a");
			break;
		}
	}
	return 0;
}

Expansion of the shutdown command

Guess you like

Origin blog.csdn.net/m0_68662723/article/details/131805252