单|双|多分支、switch、while、for、范围for循环
#include
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;
}
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;
}
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 (int i = 0; i < 10; i++)
{
cout << i << "次循环" <<endl;
}
for (int num : {1, 3, 5, 7, 9})
{
cout << "序列中输出的数据是" << num << endl;
}
cin.get();
cin.get();
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
continue
for (int num = 0; num < 101; num++)
{
cout << "\t";
if (num % 7 == 0 || num % 10 == 7 || num / 10 == 7)
{
continue;
}
cout << num;
}
goto
int x = 0;
cout << "循环开始。。。" << endl;
begin:
do
{
cout << x++ << "次循环" << endl;
} while (x <= 10);
if (x < 15)
{
cout << "回到原点" << endl;
goto begin;
}