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

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:

主站蜘蛛池模板: 莲花县| 太白县| 东明县| 焦作市| 旬邑县| 贵定县| 聂荣县| 黄陵县| 阿克陶县| 彰武县| 隆德县| 营山县| 博罗县| 铁力市| 正定县| 集贤县| 女性| 渝北区| 吉林省| 刚察县| 泰州市| 壤塘县| 山西省| 沧源| 扶沟县| 西平县| 简阳市| 平塘县| 乌海市| 封开县| 湘潭市| 错那县| 兴和县| 平乡县| 九江市| 宁都县| 东乡县| 渑池县| 荥阳市| 莆田市| 车致|