当前位置:网站首页>The difference and usage between continue and break

The difference and usage between continue and break

2022-07-06 05:51:00 bit..

  generally speaking , After the program enters the loop All statements of the loop body will be executed between the next loop test .continue and break Statement can ignore part of the loop content according to the test results in the loop body , Even the end of the cycle .

continue sentence

  All three cycles can be used continue sentence . When the statement is executed , Will skip this iteration ( It's a cycle ) The rest of , And start the next iteration . If continue Statement in nested loop , Only the inner loop containing the statement will be affected .

grammar

C In language  continue  Sentence syntax :

continue;

df0ac93a0e574d45a36fb070e0cf8cfc.jpg

  flow chart

83f56300c6e64f798ba4b65dcbb8636d.jpg

example

#include <stdio.h>

int main ()

{

   /* Definition of local variables */

   int a = 10;

   /* do Loop execution */

   do

   {

      if( a == 15)

      {

         /* Skip iteration */

         a = a + 1;

         continue;

      }

     printf("a Value : %d\n", a);

  a++;

   }while( a < 20 );

   return 0;

}

When the above code is compiled and executed , It will produce the following results :

a Value : 10

a Value : 11

a Value : 12

a Value : 13

a Value : 14

a Value : 16

a Value : 17

a Value : 18

a Value : 19

6282f54911144be6994e81f6c278b7c7.png

 

break sentence

C In language break Statement has the following two uses :

When break When a statement appears in a loop , The loop ends immediately , And the program flow continues to execute the next statement that follows the loop .

It can be used to terminate switch One of the sentences case.

grammar

C In language break Sentence syntax :

break;

90100e13b56746ffae44a8606d73f294.jpg

  flow chart

afb14f6fe6ed4e0ea957e4d673723ded.jpg

example

#include <stdio.h>

int main ()

{

   /* Definition of local variables */

   int a = 10;

   /* while Loop execution */

   while( a < 20 )

   {

      printf("a Value : %d\n", a);

      a++;

      if( a > 15)

      {

         /* Use break Statement to terminate the loop */

          break;

      }

   }

   return 0;

}

When the above code is compiled and executed , It will produce the following results :

a Value : 10

a Value : 11

a Value : 12

a Value : 13

a Value : 14

a Value : 15

 

 

原网站

版权声明
本文为[bit..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060544179178.html