Home > Article > Backend Development > The continue statement is only used in loop statements. What is its function?
The continue statement is only used in loop statements. Its function is to skip the remaining statements in the loop body and force entry into the next loop. The continue statement is only used in while and for loops, and is often used together with if conditional statements to determine whether the conditions are true.
The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.
The continue statement is only used in while and for loops. Its function is to skip the remaining statements in the loop body and force entry into the next loop.
For the while loop, continue to solve the loop condition. For the for loop program flow, the third part of the expression in the for statement header is then solved.
Flowchart:
Example:
#include <stdio.h> int main(){ char c = 0; while(c!='\n'){ //回车键结束循环 c=getchar(); if(c=='4' || c=='5'){ //按下的是数字键4或5 continue; //跳过当次循环,进入下次循环 } putchar(c); } return 0; }
Run result:
0123456789↙ 01236789
When the program encounters while, the value of variable c is '\0', the loop condition c!='\n' is established, and the first loop starts. getchar() causes the program to pause execution, wait for user input, and does not start reading characters until the user presses the Enter key.
In this example, we input 0123456789. When 4 or 5 is read, the if condition c=='4'||c=='5' is established, and the continue statement is executed to end the current loop. , directly enter the next loop, which means putchar(c); will not be executed. When other numbers are read, the if condition does not hold, the continue statement will not be executed, and putchar(c); will output the read characters.
For more knowledge about computer programming, please visit: Introduction to Programming! !
The above is the detailed content of The continue statement is only used in loop statements. What is its function?. For more information, please follow other related articles on the PHP Chinese website!