c++学习笔记——break和continue语句

 break语句使程序跳到Switch或者循环后面的语句处执行,意思就是终止整个循环。
continue的作用是使程序跳过循环体中余下的代码,并开始新一轮循环。

#include <iostream>
const int ArSize = 80;

int main()
{
    using namespace std;
    char line[ArSize];
    int spaces = 0;

    cout << "Enter a line of text.\n";
    cin.get(line, ArSize);
    cout << "Complete line.\n" << line << endl;
    cout << "Line through first period.\n";
    for (int i = 0; line[i] != '\0'; i++)
    {
        cout << line[i];
        if (line[i] == '.')
            break;
        if (line[i] != ' ')
            continue;
        spaces++;
        //虽然continue语句导致该程序跳过循环体的剩余部分,但是不会跳过循环的更新表达式。
        //在for循环中,continue语句使程序直接跳到更新表达式(这里是i++),然后跳到测试表达式处。
        //while循环中位于continue之后的更新表达式都将被跳过。
    }
    cout << "\n" << spaces << " spaces\n";
    cout << "Done.\n";
    return 0; 
}
Enter a line of text.
yang guo hello.
Complete line.
yang guo hello.
Line through first period.
yang guo hello.
2 spaces
Done.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值