比较break语句与continue语句的不同用法

比较break语句与continue语句的不同用法。

break;用法

break使程序从循环体和switch语句内跳出,继续执行逻辑上的下一条语句,不能用在别处;
例:

# include <iostream>
# include <cstdlib>

using namespace std;
int main()
{
	char choice;
	while (1)
	{
		cout << "Menu:A(dd)D(elete)S(ort)Q(uit),Select one:";
		cin >> choice;
		switch (toupper(choice))
		{
		case 'A':
			cout << "数据已经增加." << endl;
			break;
		case 'D':
			cout << "数据已经删除." << endl; 
				break;
		case 'S':
			cout << "数据已经排序." << endl; break;
		case 'Q':
			exit(0);
			break;
		default:
			;
		}
	}
	return 0;
}

continue;用法

continue 语句结束本次循环,接着开始判断决定是否继续执行下一次循环。
例:

# include <iostream>
# include <cstdlib>
using namespace std;
		
int main()
{
	char choice, c;
	while (1)
	{
		cout << "Menu:A(dd)D(elete)S(ort)Q(uit),Select one:";
		cin >> c;
		choice = toupper(c);
		if (choice == 'A')
		{
			cout << "数据已经增加." << endl;
			continue;
		}
		else if (choice == 'D')
		{
			cout << "数据已经删除." << endl;
			continue;
		}
		else if (choice == 'S')
		{
			cout << "数据已经排序." << endl;
			continue;
		}
		else if (choice == 'Q')
			break;
	}
	return 0;
}

例题选自《C++语言程序设计(第四版)》
文档供本人学习笔记使用,仅供参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NI'CE'XIAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值