本文主要是介绍C++学习4(for循环语句、跳转语句),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <iostream>
using namespace std;
int main()
{
//for循环语句
//打印从0-9这10 个数字
cout << "打印10个数字" << endl;
for(int i=0;i<10;i++)
{
cout << i << " ";
}
cout << endl;
cout << "\n " << endl;
//练习案例 1-100,如果个位含有7或者十位含有7,或者该数字是7的倍数,
//我们打印敲桌子,,其余数字直接打印输出
cout << "for循环练习案例" << endl;
for (int i1 = 0;i1 < 100;i1++)
{
int a, b, c;
a = i1 % 7;
b = i1 % 10;
c = i1 / 10;
if(a==0||b==7||c==7)//判断是否相等用两个==一个=是赋值
{
cout << "敲桌子" << " ";
}
else
{
cout << " " << i1 << " ";
}
}
cout << endl;
//嵌套循环 打印星图
cout << "\n " << endl;
cout << "打印星图" << endl;
//外层循环,内层执行一轮,外层执行一次
for (int i2 = 0;i2 < 10;i2++)
{
//内层循环
for (int i3 = 0;i3 < 10;i3++) {
cout << "* ";
}
cout << endl;
}
//练习案例
cout << "\n " << endl;
cout << "练习案例打印99乘法表" << endl;
for (int i4=1;i4<10;i4++)
{
cout << i4 << endl;
for (int j4 = 1;j4 <= i4;j4++)
{
cout << j4 << " * " << i4 << " = " << j4 * i4 << " ";
}
cout << endl;
}
cout << "\n " << endl;
//跳转语句
//break语句 1出现在switch条件语句中
//2出现在循环语句,跳出循环
//3出现在嵌套循环中,跳出循环(不举例)
//continue
cout << "continue语句示例" << endl;
for (int i5 = 0;i5 <= 100;i5++)
{
//如果是奇数输出,偶数不输出
if (0 == i5 % 2)
{
continue;
//此处可以筛选条件,执行到此就不再执行本次循环,执行下一次循环,不会使循环终止
//break则退出循环不再执行循环语句,终止循环
}
cout << i5 << " ";
}
cout << endl;
cout << "\n " << endl;
//goto 语句,无条件跳珠语句 goto 标记; (标记一般为大写字母)
//程序中不建议使用goto语句,会导致程序逻辑混乱
cout << "goto语句示例" << endl;
cout << "1 ******" << endl;
goto FLAG;
cout << "2 ******" << endl;
cout << "3 ******" << endl;
FLAG:
cout << "4 ******" << endl;
cout << "5 ******" << endl;
system("pause");
return 0;
}
这篇关于C++学习4(for循环语句、跳转语句)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!