C++編程中break語句和continue語句的學(xué)習(xí)教程
break 語句
break 語句可終止執(zhí)行最近的封閉循環(huán)或其所在條件語句。 控制權(quán)將傳遞給該語句結(jié)束之后的語句(如果有的話)。
break;
備注
break 語句與 switch 條件語句以及 do、for 和 while 循環(huán)語句配合使用。
在 switch 語句中,break 語句將導(dǎo)致程序執(zhí)行 switch 語句之外的下一語句。 如果沒有 break 語句,則將執(zhí)行從匹配的 case 標(biāo)簽到 switch 語句末尾之間的每個語句,包括 default 子句。
在循環(huán)中,break 語句將終止執(zhí)行最近的 do、for 或 while 封閉語句。 控制權(quán)將傳遞給終止語句之后的語句(如果有的話)。
在嵌套語句中,break 語句只終止直接包圍它的 do、for、switch 或 while 語句。 你可以使用 return 或 goto 語句從較深嵌套的結(jié)構(gòu)轉(zhuǎn)移控制權(quán)。
示例
以下代碼演示如何在 for 循環(huán)中使用 break 語句。
#include <iostream>
using namespace std;
int main()
{
  // An example of a standard for loop
  for (int i = 1; i < 10; i++)
  {
    cout << i << '\n';
    if (i == 4)
      break;
  }
  // An example of a range-based for loop
int nums []{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  for (int i : nums) {
    if (i == 4) {
      break;
    }
    cout << i << '\n';
  }
}
在每個用例中:
1 2 3
以下代碼演示如何在 while 循環(huán)和 do 循環(huán)中使用 break。
#include <iostream>
using namespace std;
int main() {
  int i = 0;
  while (i < 10) {
    if (i == 4) {
      break;
    }
    cout << i << '\n';
    i++;
  }
  i = 0;
  do {
    if (i == 4) {
      break;
    }
    cout << i << '\n';
    i++;
  } while (i < 10);
}
在每個用例中:
0 1 2 3
以下代碼演示如何在 switch 語句中使用 break。 如果你要分別處理每個用例,則必須在每個用例中使用 break;如果不使用 break,則執(zhí)行下一用例中的代碼。
#include <iostream>
using namespace std;
enum Suit{ Diamonds, Hearts, Clubs, Spades };
int main() {
  Suit hand;
  . . .
  // Assume that some enum value is set for hand
  // In this example, each case is handled separately
  switch (hand)
  {
  case Diamonds:
    cout << "got Diamonds \n";
    break;
  case Hearts:
    cout << "got Hearts \n";
    break;
  case Clubs:
    cout << "got Clubs \n";
    break;
  case Spades:
    cout << "got Spades \n";
    break;
  default: 
     cout << "didn't get card \n";
  }
  // In this example, Diamonds and Hearts are handled one way, and
  // Clubs, Spades, and the default value are handled another way
  switch (hand)
  {
  case Diamonds:
  case Hearts:
    cout << "got a red card \n";
    break;
  case Clubs:
  case Spades: 
  default:
    cout << "didn't get a red card \n";
  }
}
continue 語句
強制轉(zhuǎn)移對最小封閉 do、for 或 while 循環(huán)的控制表達(dá)式的控制。
語法
continue;
備注
將不會執(zhí)行當(dāng)前迭代中的所有剩余語句。確定循環(huán)的下一次迭代,如下所示:
在 do 或 while 循環(huán)中,下一個迭代首先會重新計算 do 或 while 語句的控制表達(dá)式。
在 for 循環(huán)中(使用語法 for(init-expr; cond-expr; loop-expr)),將執(zhí)行 loop-expr 子句。然后,重新計算 cond-expr 子句,并根據(jù)結(jié)果確定該循環(huán)結(jié)束還是進(jìn)行另一個迭代。
下面的示例演示了如何使用 continue 語句跳過代碼部分并啟動循環(huán)的下一個迭代。
// continue_statement.cpp
#include <stdio.h>
int main()
{
  int i = 0;
  do
  {
    i++;
    printf_s("在繼續(xù)之前\n");
    continue;
    printf("在繼續(xù)之后,不被輸出\n");
   } while (i < 3);
   printf_s("在do循環(huán)之后\n");
}
輸出:
在繼續(xù)之前 在繼續(xù)之前 在繼續(xù)之前 在do循環(huán)之后
上一篇:c++中struct使用注意事項
欄 目:C語言
下一篇:C++編程中私有和保護(hù)以及公有的類成員訪問控制
本文標(biāo)題:C++編程中break語句和continue語句的學(xué)習(xí)教程
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2542.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10使用C++實現(xiàn)全排列算法的方法詳解
 - 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
 - 01-10APUE筆記之:進(jìn)程環(huán)境詳解
 - 01-10c++中inline的用法分析
 - 01-10如何尋找數(shù)組中的第二大數(shù)
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10C#中split用法實例總結(jié)
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-10delphi制作wav文件的方法
 - 04-02jquery與jsp,用jquery
 


