c++ break的使用时机

凯约

关注

阅读 53

2022-02-06

1.出现在switch语句中

#include<iostream>
using namespace std;
int main()
{
	cout <<"请选择副本难度" << endl;//给用户输入提示
	cout << "困难1" << endl;
	cout << "中等2" << endl;
	cout << "普通3" << endl;
	int select = 0;//创建结果的变量
	cin >> select;//等待用户输入
	switch (select)
	{
	case 1://case和数字中间要有空格" "
		cout << "您选择的等级是困难" << endl;
		break;
	case 2:
		cout << "您选择的等级是中等" << endl;
		break;
	case 3:
		cout << "您选择的等级是普通" << endl;
		break;
	default:
		cout << "您输入的等级有误" << endl;
	
	}
}

2.出现在嵌套语句中

#include<iostream>
using namespace std;
int main()
{
	int i, j;
	for (i = 0; i <= 10; i++)
	{
		for (j = 0; j <= 10; j++)
		{
			if (j == 5)
			{
				break;
			}
			cout << "*";
		}
		cout << endl;
	}
}

3.出现在循环语句中

#include<iostream>
using namespace std;
int main()
{
	int i;
	for (i = 0; i <= 10; i++)
	{
			if (i == 5)
			{
				break;
			}
			cout << i << endl;
	}
}

精彩评论(0)

0 0 举报