C language break and continue usage
break is used to jump out of the switch structure. If it is in a loop statement, the break statement is used to jump out directly. Loop, and the continue statement is different from the break statement. When this statement is encountered in the loop body, this loop will be skipped and the next cycle will start.
Example
Use a while loop to calculate the value of 1 added to 100:
#include <stdio.h> int main(){ int i=1, sum=0; while(1){ //循环条件为死循环 sum+=i;i++;if(i>100) break; } printf("%d\n", sum);return 0;}
Run Result: 5050
The function of the continue statement is to skip the remaining statements in the loop body and force entry into the next loop.
#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
Recommended tutorial: "C#"
The above is the detailed content of C language break and continue usage. For more information, please follow other related articles on the PHP Chinese website!