c++语言中break的作用,C++ break和continue用法详解
用于 switch 中的 break 語(yǔ)句也可以放在循環(huán)中,當(dāng)遇到 break 時(shí),循環(huán)立即停止,程序跳轉(zhuǎn)到循環(huán)后面的語(yǔ)句。
以下是一個(gè)帶有 break 語(yǔ)句的循環(huán)示例。程序段中的 while 循環(huán)看起來(lái)要執(zhí)行 10 次,但 break 語(yǔ)句導(dǎo)致它在第 5 次迭代后即停止:
int count = 1;
while (count <= 10)
{
cout << count << endl;
count++;
if (count == 6)
break;
}
這個(gè)例子只是為了說(shuō)明在循環(huán)中的 break 語(yǔ)句的作用。通常不會(huì)有人以這種方式來(lái)使用它,因?yàn)樗`反了結(jié)構(gòu)化編程的規(guī)則,并使代碼難以理解、調(diào)試和維護(hù)。
一個(gè)循環(huán)的退出應(yīng)該通過(guò)循環(huán)頂部的條件測(cè)試來(lái)控制,就像在 while 循環(huán)或 for 循環(huán)中那樣,或者在底部,就像在 do-while 循環(huán)中那樣。通常在循環(huán)中使用 break 語(yǔ)句的唯一時(shí)間是在發(fā)生錯(cuò)誤的情況下提前退出循環(huán)。下面的程序提供了這樣一個(gè)示例:
#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;
}
程序輸出結(jié)果:
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.
在嵌套循環(huán)中使用 break
在嵌套循環(huán)中,break 語(yǔ)句只會(huì)中斷其所在位置的循環(huán)。以下程序段在屏幕上顯示 5 行星號(hào)。外部循環(huán)控制行數(shù),內(nèi)部循環(huán)控制每行中的星號(hào)數(shù)。內(nèi)部循環(huán)設(shè)計(jì)為顯示 20 個(gè)星號(hào),但是 break 語(yǔ)句使循環(huán)在第 11 次迭代中停止。
for (row = 0; row < 3; row++)
{
for (star = 0; star < 20; star++)
{
cout << '*';
if (star == 10)
break;
}
cout << endl;
}
該程序段的輸出結(jié)果如下:
***********
***********
***********
continue 語(yǔ)句
有時(shí)候可能想要保持循環(huán),但又想讓當(dāng)前迭代立即結(jié)束,這時(shí)可以通過(guò)continue 語(yǔ)句來(lái)完成。
當(dāng)遇到 continue 時(shí),出現(xiàn)在它之后的循環(huán)體中的所有語(yǔ)句都被忽略,循環(huán)準(zhǔn)備下一次迭代。在 while 循環(huán)中,這意味著程序跳轉(zhuǎn)到循環(huán)頂部的測(cè)試表達(dá)式。如果表達(dá)式仍然為 true,則下一次迭代開(kāi)始,否則,循環(huán)退出。在 do-while 循環(huán)中,程序跳轉(zhuǎn)到循環(huán)底部的測(cè)試表達(dá)式,它決定下一次迭代是否開(kāi)始。在 for 循環(huán)中,continue 會(huì)導(dǎo)致更新表達(dá)式被執(zhí)行,然后測(cè)試表達(dá)式被評(píng)估。
以下程序段表示在 while 循環(huán)中使用 continue:
int testVal = 0;
while (testVal < 10)
{
testVal++;
if (testVal) == 4
continue; //終止循環(huán)的該次迭代
cout << testVal << " ";
}
這個(gè)循環(huán)看起來(lái)像是要顯示整數(shù) 1?10。但是,其實(shí)際輸出如下:
1 2 3 5 6 7 8 9 10
請(qǐng)注意,數(shù)字未不打印。這是因?yàn)楫?dāng) testVal 等于 4 時(shí),continue 語(yǔ)句會(huì)導(dǎo)致循環(huán)跳過(guò) cout 語(yǔ)句并開(kāi)始下一次迭代。
注意,與 break 語(yǔ)句一樣,continue 語(yǔ)句違反了結(jié)構(gòu)化編程規(guī)則,使得代碼難以理解、調(diào)試和維護(hù)。因此,應(yīng)該謹(jǐn)慎使用 continue。
當(dāng)然,continue 語(yǔ)句有一些實(shí)際用途,下面的程序說(shuō)明了其中的一個(gè)應(yīng)用。該程序計(jì)算 DVD 租賃的費(fèi)用,current releases 版本費(fèi)用為 3.50 美元,所有其他版本費(fèi)用為 2.50 美元。如果一個(gè)客戶租了幾張 DVD,每 3 張有 1 張是免費(fèi)的。continue 語(yǔ)句用于跳過(guò)計(jì)算每個(gè)第 3 張 DVD 費(fèi)用的循環(huán)部分。
#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;
}
程序輸出結(jié)果:
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
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的c++语言中break的作用,C++ break和continue用法详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 复选框怎么点td选中_jQuery点击t
- 下一篇: python实现地牢迷宫生成