C++ break语句,continue语句,goto语句

亦凉 2022-09-02 12:46 432阅读 0赞

break 语句的作用:

跳出当前循环,中断当前循环

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. for (int i = 0; i < 10; i++)
  6. {
  7. cout << i << endl;
  8. if (i==5)
  9. {
  10. break;
  11. }
  12. }
  13. return 0;
  14. }

打印结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMjEwMDQy_size_16_color_FFFFFF_t_70

continue语句的作用:

跳过本次循环, 整体循环继续.

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. for (int i = 0; i < 10; i++)
  6. {
  7. if (i==5)
  8. {
  9. continue;
  10. }
  11. cout << i << endl;
  12. }
  13. return 0;
  14. }

打印结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMjEwMDQy_size_16_color_FFFFFF_t_70 1

goto语句的作用:

改变语句执行的顺序,结束循环.

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. for (int i = 0; i < 10; i++)
  6. {
  7. cout << i << endl;
  8. goto label;
  9. }
  10. label:
  11. cout << "11111" << endl;
  12. return 0;
  13. }

打印结果:

2021080313035825.png

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读

    相关 goto语句

    对于goto语句,在c/c++中就已经被不推荐使用了,在java中根本就没有goto的存在了,为什么c\中又重新使用goto语句呢? 首先了解一下c\中的goto语句