Home >Backend Development >C#.Net Tutorial >What is the function of continue in C language?
The function of the continue statement is to skip the remaining statements in the loop body, end the loop, and force entry into the next loop.
The continue statement is only used in while and for loops, and is often used together with the if conditional statement to determine whether the condition is true.
Let's look at an 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; }
Analysis:
When the program encounters while, the value of variable c is '\0', and the loop condition c!=' \n' is established and the first cycle 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.
For example, what we input is 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. Enter the next loop directly, 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.
Recommended tutorial: C language tutorial
The above is the detailed content of What is the function of continue in C language?. For more information, please follow other related articles on the PHP Chinese website!