Home  >  Article  >  Backend Development  >  C language break and continue usage

C language break and continue usage

Guanhui
GuanhuiOriginal
2020-06-03 16:49:345294browse

C language break and continue usage

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!=&#39;\n&#39;){  //回车键结束循环
        c=getchar();
        if(c==&#39;4&#39; || c==&#39;5&#39;){  //按下的是数字键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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn