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

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:

主站蜘蛛池模板: 桓台县| 东阳市| 鄄城县| 卓尼县| 宜君县| 湖北省| 四子王旗| 玛沁县| 天镇县| 楚雄市| 宝兴县| 固安县| 常熟市| 涟源市| 隆德县| 沙雅县| 徐闻县| 平谷区| 上犹县| 许昌市| 湛江市| 达日县| 永宁县| 睢宁县| 建德市| 鸡西市| 建德市| 青海省| 卓尼县| 长武县| 邵阳市| 湘潭县| 微山县| 安庆市| 舞钢市| 凌源市| 桑日县| 宁阳县| 民乐县| 海林市| 彩票|