单|双|多分支、switch、while、for、范围for循环
#include<iostream>
using namespace std;
int main()
{
// 单分支和双分支
cout << "please enter your age:" << endl;
int age;
cin >> age;
if(age >= 18)
{
cout << "welcome! adult." << endl;
}
else
{
cout << "only adult can pass." << endl;
}
age >= 18 ? cout << "welcome, adult!" << endl : cout << "only adult can pass" << endl;
cout << (age >= 18 ? "welcome, adult!" : "only adult can pass!") << endl;
//多分支
cout << "please enter your age:" << endl;
int age;
cin >> age;
if (age < 12)
{
cout << "child" << endl;
}
else if (age <= 18)
{
cout << "teenager" << endl;
}
else if (age <= 35)
{
cout << "keep going" << endl;
}
else
{
cout << "enjoy your life" << endl;
}
// switch
cout << "please enter your score level:" << endl;
char score;
cin >> score;
switch (score)
{
case 'A':
cout << "score >= 90 && score <= 100" << endl;
break;
case 'B':
cout << "score >= 80 && score < 90" << endl;
break;
case 'C':
cout << "score < 80" << endl;
break;
default:
cout << "wrong score" << endl;
break;
}
// while
cout << "loop strat...\n" << endl;
int i = 1;
while(i <= 10)
{
cout << "hello world" << i++ << endl;
}
do while
int i = 10;
do
{
cout << i-- << "次循环" << endl;
} while (i > 0);
//for循环
for (int i = 0; i < 10; i++)
{
cout << i << "次循环" <<endl;
}
//范围for循环
for (int num : {1, 3, 5, 7, 9})
{
cout << "序列中输出的数据是" << num << endl;
}
cin.get();
cin.get();
}
continue
// continue
// 逢7过
for (int num = 0; num < 101; num++)
{
cout << "\t";
if (num % 7 == 0 || num % 10 == 7 || num / 10 == 7)
{
continue;
}
cout << num;
}
goto
// goto
int x = 0;
cout << "循环开始。。。" << endl;
begin:
do
{
cout << x++ << "次循环" << endl;
} while (x <= 10);
if (x < 15)
{
cout << "回到原点" << endl;
goto begin;
}
文章来源地址https://www.toymoban.com/news/detail-720269.html
文章来源:https://www.toymoban.com/news/detail-720269.html
到了这里,关于C++ 流程控制(分支、循环、跳转)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!