欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++編程中break語句和continue語句的學(xué)習(xí)教程

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

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 語句
強(qiáng)制轉(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://mengdiqiu.com.cn/a1/Cyuyan/2542.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有