官术网_书友最值得收藏!

Jump statements

As well as condition and iteration statements, you also have break and continue statements.

The break statement is used to break out of an iteration. We can leave a loop and force it to quit if a certain condition is met.

Let's look at the break statement in use:

#include <iostream> 
#include <conio.h>  
// Program prints out values to screen  
int main() 
{  
   for (int n = 0; n < 10; n++) 
   {         
         if (n == 5) {               
               std::cout << "break" << std::endl; 
               break; 
         } 
         std::cout << "value of n is: " << n << std::endl; 
   }  
   _getch(); 
   return 0; 
} 

The output of this is as follows:

The continue statement will skip the current iteration and continue the execution of the statement until the end of the loop. In the break code, replace the break with continue to see the difference:

#include <iostream> 
#include <conio.h> 
 
// Program prints out values to screen 
 
int main() 
{ 
 
   for (int n = 0; n < 10; n++) 
   { 
         if (n == 5) { 
                
               std::cout << "continue" << std::endl; 
 
               continue; 
         } 
         std::cout << "value of n is: " << n << std::endl; 
   } 
   _getch(); 
   return 0; 
} 

Here is the output when break is replaced with continue:

主站蜘蛛池模板: 密山市| 永清县| 塔河县| 类乌齐县| 襄垣县| 赫章县| 绍兴市| 公安县| 鞍山市| 中阳县| 微山县| 渭源县| 锡林郭勒盟| 桑日县| 丹棱县| 翁牛特旗| 大新县| 江门市| 蚌埠市| 手游| 平凉市| 清镇市| 乳源| 克什克腾旗| 洛扎县| 新干县| 攀枝花市| 台南市| 龙州县| 突泉县| 彰武县| 福泉市| 黑水县| 嵩明县| 屏东县| 蒙城县| 崇州市| 五大连池市| 湄潭县| 康定县| 漳州市|