The C language learning and comprehension combing record with personal understanding as the main body focuses on better understanding some concepts. There must be something that is not right. Please help me point out the mistakes when you forgive me! Grateful!
Table of contents
Functional overview of the while statement
Functional overview of the do while statement
for statement overview and function
Loop statement is a very practical method in C language. It can not only print out the changes of variables in each loop, but also use loops to realize some other functions, such as guessing games.
However, before writing a guessing game, it is difficult for a clever woman to cook without rice, and she must first understand the types and usage methods of loop statements.
Functional overview of the while statement
while(值满足测试条件)
{
处理该值
获取下一个值
}
while, its operation process is very simple, if you know the story of Sisyphus, it will not be difficult.
Sisyphus was punished for pushing a boulder up a hill every day, and when the boulder reached the top, his punishment was over, but he was cursed so that whenever the boulder was about to reach the top, his boulder would roll straight back anyway Go down the mountain and repeat.
The while statement is like this story. While () the bracket content statement continues to execute when it is true, and jumps out of the loop when it is false. C handles test conditions by evaluating them to 0 for false and non-zero for true.
For Sisyphus, his tragic fate can be described in this way by the while statement.
while(巨石没有被推上山顶)
{
继续推动巨石;
}
惩罚结束;
When the parentheses of the while statement are false, that is, "the boulder is pushed to the top of the mountain", the while loop ends, jumps out, and ends with punishment.
But poor Sisyphus in his cycle of fate has absolutely no chance of changing the truth or falsehood of the judgment condition! This time makes his fate enter an infinite loop like the following while statement.
int main()
{
int a = 2;
while (a < 10)
{
printf("推动巨石\n");
}
return 0;
}
Therefore, if each loop statement does not want it to fall into an infinite loop, it must provide a method or condition that can satisfy the judgment expression. We can use the characteristics of the loop to make the variable a gradually approach the condition that can jump out of the loop and jump out of the loop.
That's very simple! We use the nature of the loop, and a increments by 1 every time we loop
int main()
{
int a = 2;
while (a < 10)
{
printf("推动巨石\n");
a++;
}
return 0;
}
Of course, the while statement will check whether the running code satisfies the judgment expression within its scope, but there is a small problem . If while encounters a situation that makes the judgment condition false, will it jump out of the loop immediately? Will the rest of the statements still be executed?
Please observe the following code.
int main()
{
int a = 2;
while (a == 2)
{
printf("a的值现在是%d\n", a);
a = 1;
printf("a的值现在是%d", a);
}
}
We modified the value of a halfway to make the judgment condition false, and the running results are as follows
It can be seen that the condition of while is judged to be true or false after each loop ends.
Functional overview of the do while statement
If you are having a headache "Why do you have to satisfy the judgment expression of while to enter the loop?!", just find do while.
The do while statement, the code under the do statement will be executed once first, and then the while judgment will be made. All in all, do while guarantees that the content in the loop statement can be executed at least once , instead of executing while satisfying.
do
{
需要执行的循环语句;
}
while(判断表达式)
(DO IT!!!)
for statement overview and function
The for statement can be said to be a very practical loop statement. Compared with the while statement, for realizes the controllability of the loop.
for(获得初值;值满足测试条件;获得下一个值)
{
处理该值
}
It seems a bit incomprehensible, let's be more intuitive
int main()
{
int n = 5;
for (n = 1; n <= 10; n++)
{
printf("这是第%d次打印\n", n);
}
return 0;
}
Summarizing the performance of the above for, you can better refine the following for functions for (initialization; test; update)
The expressions in it can also be more complicated, depending on the application scenario, we may have some difficulty in choosing between the for statement or the while statement, but generally speaking, it mainly depends on the problem to be solved. If initialization is required It is more intuitive to use the for statement when it is numerical.
To sum up, loop is a very powerful programming tool, but the most important principle to control this tool is: don't loop endlessly!
in conclusion
1. Pay attention to the test condition of the loop to make the loop end (otherwise the story of Sisyphus will be repeated)
2. Make sure that the value of the loop test is initialized before the first use (if the initialization statement is placed in the loop statement, the variable will be reinitialized every time the loop is run)
3. Ensure that the value of the test can be updated every cycle (the cycle can only be terminated if there is a change)
goto statement (portal)
Reminder in the front row, since the goto statement is easy to disrupt the normal loop logic and cause errors, it is best to avoid using it.
goto is very straightforward and simple, "Just go there!"
The usage is as follows
Use goto to draw up a name, mark the exit like a teleportation gun, and put this name in a place in the program
Jump to the part of the name, that is, add a colon after the mark:
mark//标记出口
执行程序
goto mark//标记入口
This program is an endless loop like a portal.
After the program is executed, once a goto is encountered, the program behind the goto will be ignored, so this is the reason why it is easy to disrupt the logic. The more common method of goto is to jump out of a deeply nested loop. (no narration)