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() {
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循环语句
注意:与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循环
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语句时,会跳转到标记的位置
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; }
|