c++初学笔记(2)

for循环

for(自变量定义【0】;变量范围定义【1】;变量处理【3】)
{
   cout << i << endl;【2】
}

练习(敲桌子)

#include<iostream>
using namespace std;

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

嵌套循环

//外层循环循环一次,内层循环循环一周
for()//内层循环
{
  for()//外层循环
  {

  }
}
#include<iostream>
using namespace std;

int main()
{
	for (int i =0 ; i < 10;i++)
	{
		for (int i = 0;i < 10;i++)
		{
			cout << "* ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

练习(乘法口诀表)

#include<iostream>
using namespace std;

int main()
{
	for (int i = 1; i < 10;i++)
	{
		for (int j = 1;j <=i;j++)
		{
			cout << j << "*" << i << "=" << j * i <<"   ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

跳转语句

break语句

//break 的使用时机
//1.出现在switch语句中
//2.出现在循环语句中
for (int i = 0;i < 10; i++)
{
	//如果i等于5,退出循环,不再打印
	if (i == 5)
	{
		break;//退出循环
	}
	cout << i << endl;
}
//3.出现在嵌套循环语句中

continue语句

执行到本行,就不再执行下面的代码,而执行下一次循环

goto语句

#include<iostream>
using namespace std;

int main()
{
	cout << 1. << endl;
	cout << 2. << endl;
	goto FLAG;
	cout << 3. << endl;
	cout << 4. << endl;
	FLAG:
	cout << 5. << endl;
	system("pause");
	return 0;
}

不推荐使用,阅读起来有困难

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值