C++基础(3)——for循环案例

目录

1.拍桌子游戏

2.嵌套循环

3.break与continue          

4.goto语句


1.拍桌子游戏

#include<iostream>
using namespace std;

int main() {
	for (int i = 1; i <= 100; i++) {
		if (i % 10 == 7 || i / 10 == 7 || i % 7 == 0) {
			std::cout << "敲桌子" << endl;
		}
		else {
			std::cout << i << endl;
		
		}
	}
	return 0;
}

2.嵌套循环

 打印星图

#include<iostream>
using namespace std;

void main() {
	for (int i = 0; i <= 10; i++) {
		for (int j = 0; j <= 10; j++) {
			cout << " *";
		}
		cout << endl;
	}
}

 

 案例乘法口诀表:

#include<iostream>
using namespace std;

void main() {
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= i; j++) {
			cout << j<<"*"<<i<<"=" << i * j<<"   ";
		}
		cout << endl;
	
	}
}

3.break与continue          

       break与continue的区别是,执行到break直接退出循环,而continue只是结束当前的循环,继续执行后面的循坏。 

首先看break案例:

#include<iostream>
using namespace std;

void main() {
	cout << "请选择副本难度" << endl;
	cout << "1.普通难度" << endl;
	cout << "2.中等难度" << endl;
	cout << "3.困难" << endl;
	int select=0;
	cin >> select;
	switch (select)
	{
	case 1:
		cout << "你选择的是普通难度" << endl;
		break;//如果不加break在选取难度的时候就会都输出
	case 2:
		cout << "你输入的是中等难度" << endl;
		break;
	case 3:
		cout << "你输入的是高等难度" << endl;
		break;
	default:
		break;
	}
}

下面关于continue的案例:

我们要求0-100,如果是奇数就输出,偶数不输出

#include<iostream>
using namespace std;

void main() {
	for (int i = 1; i < 100; i++) {
		if (i % 2 == 0) {
			continue;
		}
		else
		{
			std::cout << i << endl;
		}
	}
}

 

4.goto语句

         可以物体哦见的跳转语句。

         语法:goto 标记;

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

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI炮灰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值