51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

【C++学习】程序流程结构

switch语句 {#switch语句}

|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | int main() { //请给电影评分 //10 ~ 9 经典 // 8 ~ 7 非常好 // 6 ~ 5 一般 // 5分以下 烂片 int score = 0; cout << "请给电影打分" << endl; cin >> score; switch (score) { case 10: case 9: cout << "经典" << endl; break; case 8: cout << "非常好" << endl; break; case 7: case 6: cout << "一般" << endl; break; default: cout << "烂片" << endl; break; } system("pause"); return 0; } |

  • switch语句中表达式类型只能是整型或者字符型
  • case里如果没有break,那么程序会一直向下执行

do...while循环语句 {#do…while循环语句}

**注意:**与while的区别在于do...while会先执行一次循环语句,再判断循环条件

|------------------------------------------------|---------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | int main() { int num = 0; do { cout << num << endl; num++; } while (num < 10); system("pause"); return 0; } |

for循环 {#for循环}

continue语句 {#continue语句}

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

|---------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int main() { for (int i = 0; i < 100; i++) { if (i % 2 == 0) { continue; } cout << i << endl; } system("pause"); return 0; } |

goto语句 {#goto语句}

**作用:**可以无条件跳转语句

**解释:**如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

|---------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 | int main() { cout << "1" << endl; goto FLAG; cout << "2" << endl; cout << "3" << endl; cout << "4" << endl; FLAG: cout << "5" << endl; system("pause"); return 0; } |

赞(1)
未经允许不得转载:工具盒子 » 【C++学习】程序流程结构