1、与switch语句联合使用
C++入门学习(二十三)选择结构-switch语句-CSDN博客
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
int number;
cout<<"请为《斗萝大路》打星(1~5※):" <<endl;
cin>>number;
switch (number) {
case 1:
cout<<"*"<<endl;
break;
case 2:
cout<<"**"<<endl;
break;
case 3:
cout<<"***"<<endl;
break;
case 4:
cout<<"****"<<endl;
break;
case 5:
cout<<"*****"<<endl;
break;
default:
cout<<"没有这个分数"<<endl;
}
return 0;
}
如果没有break,会产生怎样的效果呢?我将上述代码中的break都删除一了,看一下结果吧:

2、与for循环结合起来
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i==6)
{
cout<<"666"<<endl;
break;
}
cout <<i<<endl;
}
return 0;
}
