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;
}
不推荐使用,阅读起来有困难