Home  >  Article  >  Backend Development  >  How to use break in c language

How to use break in c language

尚
Original
2020-03-03 10:45:1719455browse

How to use break in c language

The break statement in C language has the following two uses:

1. When the break statement appears within a loop, the loop will terminate immediately, and the program flow will Execution continues with the next statement immediately following the loop.

2. It can be used to terminate a case in a switch statement.

If you are using nested loops (that is, one loop nested within another loop), the break statement will stop execution of the innermost loop and then start execution of the next line of code after the block.

Example:

#include<stdio.h>
int main()
{
/*局部变量定义*/
int a=10;
/*while循环执行*/
while(a<20)
{
printf("a的值:%d\n",a);
a++;
if(a>15)
{
/*使用break语句终止循环*/
break;
}
}
return 0;
}

When the above code is compiled and executed, it produces the following results:

Value of a: 10

a's value: 11

a's value: 12

a's value: 13

a's value: 14

a's value: 15

Recommended: "c Language Tutorial"

The above is the detailed content of How to use break in c language. 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
Previous article:what is dev c++Next article:what is dev c++