c++语言中break的作用,C++ break和continue用法详解
用于 switch 中的 break 語句也可以放在循環中,當遇到 break 時,循環立即停止,程序跳轉到循環后面的語句。
以下是一個帶有 break 語句的循環示例。程序段中的 while 循環看起來要執行 10 次,但 break 語句導致它在第 5 次迭代后即停止:
int count = 1;
while (count <= 10)
{
cout << count << endl;
count++;
if (count == 6)
break;
}
這個例子只是為了說明在循環中的 break 語句的作用。通常不會有人以這種方式來使用它,因為它違反了結構化編程的規則,并使代碼難以理解、調試和維護。
一個循環的退出應該通過循環頂部的條件測試來控制,就像在 while 循環或 for 循環中那樣,或者在底部,就像在 do-while 循環中那樣。通常在循環中使用 break 語句的唯一時間是在發生錯誤的情況下提前退出循環。下面的程序提供了這樣一個示例:
#include
#include
using namespace std;
int main()
{
double number;
cout << "Enter 5 positive numbers separated by spaces and \n" << "I will find their square roots: ";
for (int count = 1; count <= 5; count++)
{
cin >> number;
if (number >= 0.0)
{
cout << "\nThe square root of " << number << " is " << sqrt(number) <
}
else
{
cout << number << " is negative. " << "I cannot find the square root of a negative number. The program is terminating.\n";
break;
}
}
return 0;
}
程序輸出結果:
Enter 5 positive numbers separated by spaces and I will find their square roots: 12 15 -17 19 31
The square root of 12 is 3.4641
The square root of 15 is 3.87298
-17 is negative. I cannot find the square root of a negative number. The program is terminating.
在嵌套循環中使用 break
在嵌套循環中,break 語句只會中斷其所在位置的循環。以下程序段在屏幕上顯示 5 行星號。外部循環控制行數,內部循環控制每行中的星號數。內部循環設計為顯示 20 個星號,但是 break 語句使循環在第 11 次迭代中停止。
for (row = 0; row < 3; row++)
{
for (star = 0; star < 20; star++)
{
cout << '*';
if (star == 10)
break;
}
cout << endl;
}
該程序段的輸出結果如下:
***********
***********
***********
continue 語句
有時候可能想要保持循環,但又想讓當前迭代立即結束,這時可以通過continue 語句來完成。
當遇到 continue 時,出現在它之后的循環體中的所有語句都被忽略,循環準備下一次迭代。在 while 循環中,這意味著程序跳轉到循環頂部的測試表達式。如果表達式仍然為 true,則下一次迭代開始,否則,循環退出。在 do-while 循環中,程序跳轉到循環底部的測試表達式,它決定下一次迭代是否開始。在 for 循環中,continue 會導致更新表達式被執行,然后測試表達式被評估。
以下程序段表示在 while 循環中使用 continue:
int testVal = 0;
while (testVal < 10)
{
testVal++;
if (testVal) == 4
continue; //終止循環的該次迭代
cout << testVal << " ";
}
這個循環看起來像是要顯示整數 1?10。但是,其實際輸出如下:
1 2 3 5 6 7 8 9 10
請注意,數字未不打印。這是因為當 testVal 等于 4 時,continue 語句會導致循環跳過 cout 語句并開始下一次迭代。
注意,與 break 語句一樣,continue 語句違反了結構化編程規則,使得代碼難以理解、調試和維護。因此,應該謹慎使用 continue。
當然,continue 語句有一些實際用途,下面的程序說明了其中的一個應用。該程序計算 DVD 租賃的費用,current releases 版本費用為 3.50 美元,所有其他版本費用為 2.50 美元。如果一個客戶租了幾張 DVD,每 3 張有 1 張是免費的。continue 語句用于跳過計算每個第 3 張 DVD 費用的循環部分。
#include
#include
using namespace std;
int main()
{
int numDVDs; // Number of DVDs being rented
double total = 0.0; // Accumulates total charges for all DVDs
char current; // Current release? (Y/N)
// Get number of DVDs rented
cout << "How many DVDs are being rented?";
cin >> numDVDs;
//Determine the charges
for (int dvdCount = 1; dvdCount <= numDVDs; dvdCount++)
{
if (dvdCount % 3 == 0)// If it's a 3rd DVD itT s free
{
cout <
continue;
}
cout << "Is DVD #" << dvdCount << " a current release (Y/N) ? ";
cin ? current;
if ( (current == 'Y') || (current == 'y'))
total += 3.50;
else
total += 2.50;
}
//Display the total charges
cout << fixed << showpoint << setprecision(2);
cout << "The total is $" << total << endl;
return 0;
}
程序輸出結果:
How many DVDs are being rented? 6
Is DVD #1 a current release (Y/N) ? y
Is DVD #2 a current release (Y/N) ? n
DVD #3 is free!
Is DVD #4 a current release (Y/N)? n
Is DVD #5 a current release (Y/N)? y
DVD #6 is free!
The total is $12.00
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的c++语言中break的作用,C++ break和continue用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 复选框怎么点td选中_jQuery点击t
- 下一篇: python实现地牢迷宫生成