Latest web development tutorials

C break statement

C cycle C cycle

C languagebreak statement has the following two ways:

  1. When abreak statement occurs within a loop, the loop will be immediately terminated and the program flow will continue to implement immediately the next statement cycle.
  2. It can be used to terminatethe switch statement in a case.

If you are using a nested loop (ie a loop nested inside another loop), break statement to stop the execution of the innermost loop, and then start the next line of code after the block.

grammar

Break statement in C language syntax:

break;

flow chart

C break statement

Examples

#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:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15

C cycle C cycle