C++ break和continue用法详解

C++ break和continue用法详解,第1张

概述用于 switch 中的 break 语句也可以放在循环中,当遇到 break 时,循环立即停止,程序跳转到循环后面的语句。 以下是一个带有 break 语句的循环示例。程序段中的 while 循环看起来要执行 用于 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 <iostream>#include <cmath>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) <<endl;        }        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 <iostream>#include <iomanip>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 <<" DVD #" << dvdCount << " is free! \n";            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和continue用法详解全部内容,希望文章能够帮你解决C++ break和continue用法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231310.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存