Home > Backend Development > C#.Net Tutorial > C language break and continue usage

C language break and continue usage

Guanhui
Release: 2020-06-03 16:49:34
Original
5418 people have browsed it

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;}
Copy after login

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;
}
Copy after login

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!

Related labels:
c
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template