break语句

用于跳出选择结构或者循环结构
使用时机:

  • 出现在switch条件语句中,作用是终止case并跳出switch
  • 出现在循环语句中,作用是跳出当前的循环语句
  • 出现在嵌套循环中,跳出最近的内层循环语句
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
    for (int i = 1; i < 10; i++) {
        cout << i << endl;
        if (i == 5) {
            break;
        }
    }
    return 0;
}

continue语句

作用:在循环语句中跳过本次循环中余下尚未执行的语句,继续执行下一次循环

#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
    for (int i = 1; i < 10; i++) {
        if (i %2==0) {
            continue;
        }
        cout << i << endl;
    }
    return 0;
}

goto语句

语法:goto 标记

#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
    cout << 1 << endl;
    goto FLAG;
    cout << 2 << endl;
    cout << 3 << endl;
    FLAG:
    cout << 4 << endl;
    cout << 5 << endl;
    return 0;
}

注意:在程序中不建议使用goto语句,以免造成程序流程混乱