C++入门学习(二十七)跳转语句—break语句

深碍√TFBOYSˉ_ 2024-05-07 22:05 131阅读 0赞

1、与switch语句联合使用

C++入门学习(二十三)选择结构-switch语句-CSDN博客

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5. int number;
  6. cout<<"请为《斗萝大路》打星(1~5※):" <<endl;
  7. cin>>number;
  8. switch (number) {
  9. case 1:
  10. cout<<"*"<<endl;
  11. break;
  12. case 2:
  13. cout<<"**"<<endl;
  14. break;
  15. case 3:
  16. cout<<"***"<<endl;
  17. break;
  18. case 4:
  19. cout<<"****"<<endl;
  20. break;
  21. case 5:
  22. cout<<"*****"<<endl;
  23. break;
  24. default:
  25. cout<<"没有这个分数"<<endl;
  26. }
  27. return 0;
  28. }

如果没有break,会产生怎样的效果呢?我将上述代码中的break都删除一了,看一下结果吧:

940a12adf1c24dffb5fcc1141fc14778.png

2、与for循环结合起来

C++入门学习(二十六)for循环-CSDN博客

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

ca57da3fa16948849091cb6d04ac0dcd.png

发表评论

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

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

相关阅读

    相关 Go 控制语句-break

    一 问题引出 随机生成 1-100 的一个数,直到生成了 99 这个数,看看你一共用了几次? 1 分析 编写一个无限循环的控制,然后不停的生成随机数,当生成 99