The previous article introduced the first while loop in the branch selection statement and loop statement, and this article will continue to introduce the remaining two loops and goto statements.
1. The for loop statement
We have learned a while loop before, so why do we need to learn a for loop here?
Before answering this question, let me introduce the for loop first, and then compare the two.
grammar structure
for(expression 1; expression 2; expression 3) loop statement;
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.
Example:
Use a for loop to print the numbers 1-10 on the screen.
#include <stdio.h> int main() { int i = 0; //for(i=1/*initialization*/; i<=10/*judgment part*/; i++/*adjustment part*/) for(i=1; i<=10; i++) { printf("%d ", i); } return 0; }
Looking here, do you already have a basic understanding of for loops? Now I will answer the difference between for and while loops.
Here the same requirement is presented with two codes, please observe:
#include<stdio.h> //while loop int main() { int i = 0;//initialization part while (i < 10)//judgment part { printf("hello word\n"); i++;//adjustment part } return 0; } //for loop int main() { for (int i = 0; i < 10; i++) { printf("hello word\n"); } }
From the code point of view, while and for loops also have three necessary conditions, but due to style issues, the three parts are likely to deviate far away, so the search and modification are not concentrated and convenient enough. Therefore, the style of the for loop is better; so that the frequency of the for loop is also the highest.
break and continue in for loop
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 when implementing in the code
break
Example:
#include <stdio.h> int main() { int i = 0; for(i=1; i<=10; i++) { if(i == 5) break; printf("%d ",i); } return 0; }
result:
continue
Example:
#include <stdio.h> int main() { int i = 0; for(i=1; i<=10; i++) { if(i == 5) continue; printf("%d ",i); } return 0; }
result:
If you still have an impression of continue introduced in the while loop in the previous article, you may find that in the while loop, the result of using continue is as follows:
But in the for loop, the result is different, why is this happening?
In fact, when you really understand the command continue, you will find that the continue command skips the statement after continue, and then enters the next cycle.
Knowing this, if you carefully observe the code writing of the two loops, you will find that the adjustment part of i in the for loop is at the beginning, which means that the adjustment part of the for loop is not affected by the continue in the loop body. The adjustment part of i in the while loop is after continue, which means that if the adjustment part of the while loop is after continue, when continue is executed, the adjustment part will not execute commands, which may cause an infinite loop of the program.
Some variants of the for loop:
The first type: conditional omission
//code 1 #include <stdio.h> int main() { for(;;) { printf("hello word\n"); } //The initialization part, judgment part, and adjustment part in the for loop can be omitted, but it is not recommended to omit it for beginners, which may easily cause problems.
Example:
The second type: multivariable control loop
#include <stdio.h> int main() { //Code 4 - Control loop with extra variables int x, y; for (x = 0, y = 0; x < 2 && y < 5; ++x, y++) { printf("hehe\n"); } return 0; }
Also note:
- Do not modify the loop variable in the body of the for loop to prevent the for loop from getting out of control.
- 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".
Two, do while loop statement
grammar structure
do loop statement; while(expression);
Code demo:
#include <stdio.h> int main() { int i = 1; do { printf("%d ", i); i=i+1; }while(i<=10); return 0; }
Features of the do statement
The loop is executed at least once, and the usage scenarios are limited, so it is not often used.
break and continue in do while loop
Code demo:
#include <stdio.h> int main() { int i = 1; do { if (5 == i) break; printf("%d ", i); i = i + 1; } while (i <= 10); return 0; } #include <stdio.h> int main() { int i = 1; do { if (5 == i) continue; printf("%d ", i); i = i + 1; } while (i <= 10); return 0; }
Three, goto statement
The C language provides a free-to-abuse goto statement and the label that marks the jump.
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.
In this case, using a break in a multi-layer loop is useless. A break can only exit from the innermost loop to the previous loop.
grammar structure
... goto flag; ... flag :;
or
flag :; ... goto flag; ...
Code demo:
int main() { for (...) { for (...) { flag :; ...//The ellipsis here means omitting n nested for loops for (...) { goto flag; } } } return 0; }
The scenarios where the goto language is really suitable are as follows:
for (...) for (...) { for (...) { if (disaster) goto error; } } ... error : if (disaster) // Handle error conditions
The use of goto and break
Here is an example of using a goto statement, and then replacing the goto statement with a loop implementation:
How to write goto statement:
#include <stdio.h> int main() { int i = 0; for (i = 0; i < 10; i++) { int j = 0; for (j = 0; j < 10; j++) { if (i > 5) { goto flag; } printf("%d %d\n", i, j); } } flag:; return 0; }
for loop writing:
#include <stdio.h> int main() { int i = 0; for (i = 0; i < 10; i++) { if (i > 5) { break; } int j = 0; for (j = 0; j < 10; j++) { printf("%d %d\n", i, j); } } flag:; return 0; }
At this point in this article, the branch and loop statements are roughly explained. The next section will enter the introduction and explanation of functions in C language. Welcome to visit next time!
Hope this article is helpful to you!