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

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:

主站蜘蛛池模板: 海晏县| 托里县| 华容县| 宁强县| 民勤县| 清苑县| 米泉市| 广安市| 永吉县| 太湖县| 涞水县| 兰考县| 巴林左旗| 金川县| 乐东| 海城市| 玉山县| 同德县| 灵石县| 盱眙县| 弥渡县| 尚义县| 株洲市| 凯里市| 桃江县| 丽江市| 临潭县| 康乐县| 大关县| 乌鲁木齐市| 临沭县| 汉源县| 泰安市| 莱西市| 定南县| 绥中县| 富川| 卓尼县| 枣阳市| 东源县| 古蔺县|