preface
This paper will analyze the usage and differences between break statement and continue statement in C language through simple examples. You are welcome to criticize and correct. (some contents are slightly repeated, you can choose to jump to the directory)1, The role of both in the circulatory system
1.while loop
Let's look at an example of a cycle: This program prints the numbers 1-10 on the screen
int main() { int i = 1; while (i <= 10) { printf("%d ", i); i++; } return 0; }
Operation results:
(1) break statement
We add two lines of code to the code just now and use the break statement to turn it into the following program:int main() { int i = 1; while (i <= 10) { if (i == 5) break; printf("%d ", i); i++; } return 0; }
The running results of this program are as follows:
Analyze the running results of this program:
Summary: the break in the while loop is used to permanently terminate the loop
(2) continue statement
Again, we will replace the break statement with the continue statement
int main() { int i = 1; while (i <= 10) { if (i == 5) //break; continue; printf("%d ", i); i++; } return 0; }
Run the program and get the results:
We can see that the program is not finished. The following is an analysis of the program:
Summary: the function of the continue statement in the while loop is to terminate this loop, that is, the code behind the continue will not be executed, and the program will jump to the judgment part of the while statement. Since i is equal to 5 and the judgment condition is always satisfied and less than or equal to 10, the loop will enter an dead loop and continue all the time.
This column is not intuitive for the use of the continue statement, so we modify it, place the i + + statement in the above code at the front of the while loop, and modify the initial value of i and the loop conditions to ensure that the improved code can still print numbers 1-10 on the screen
The following is the improved code:
int main() { int i = 0; while (i < 10) { i++; if (i == 5) //break; continue; printf("%d ", i); } return 0; }
The following are the running results:
Then we add the continue statement to the code:
int main() { int i = 0; while (i <10) { i++; if (i == 5) continue; printf("%d ", i); } return 0; }
The following are the running results:
2.for loop
Still in the example just now, print the numbers 1-10 on the screen, which we implement with a for loop
int main() { int i = 0; for (i = 1; i <= 10; i++) { printf("%d ", i); } return 0; }
Program running results:
(1) break statement
code:
int main() { int i = 0; for (i = 1; i <= 10; i++) { if (i == 5) { break; } printf("%d ", i); } return 0; }
Result analysis:
Summary: like the while statement, the break in the for loop is used to permanently terminate the loop.
(2) continue statement
code:
int main() { int i = 0; for (i = 1; i <= 10; i++) { if (i == 5) { //break; continue; } printf("%d ", i); } return 0; }
Result analysis:
Summary: like the while statement, continue in the for loop is used to jump out of this loop and proceed to the next loop.
3.do while loop
Example: output 1-10
int main() { int i = 0; do { i++; printf("%d ", i); } while (i < 10); return 0; }
result:
(1) break statement
Example:
int main() { int i = 0; do { i++; if (i == 5) { break; //continue; } printf("%d ", i); } while (i < 10); return 0; }
result:
(2) continue statement
Example:
int main() { int i = 0; do { i++; if (i == 5) { //break; continue; } printf("%d ", i); } while (i < 10); return 0; }
result:
2, switch statement
The continue statement is only used in loop statements, not switch statements
The break statement can be used in the switch statement. When it is used, the program can jump out of switch and execute the statements after switch. If there is no break statement, it will be executed from the place where the condition is met (that is, the case matching the expression in the parenthesis of switch) until the end of the switch structure.
Example: input numbers 1-7, where 1-5 outputs weekday and 6 and 7 outputs weeken
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; }
result:
summary
In this paper, the break and continue statements in C language are analyzed in detail. Because there are some differences in the use of the three loops (while, for and do while), we should make specific judgment in practical application. At the same time, thank you for watching. These are all the contents of this paper.