#include <bits/stdc++.h>
using namespace std;
int main() {
// 3、循环结构
// (1) 前置条件循环 while () { } ; if () {}
int i = 1;
// i = i + 1;
while (i <= 9) {
cout << "我要上天 " << i << " 重" << endl;
i++;
}
int j = 0;
while (j < 18) {
cout << "他要入地 " << j << " 层" << endl;
j++;
}
// 高斯求和: 1 + 2 + 3 + ... + 18
// 1 = 0 + 1;
// 3 = 1 + 2;
// 6 = 3 + 3;
// 10 = 6 + 4;
int i = 1; // 循环变量 初始化
int sum = 0;
while (i <= 100) { // 循环条件
sum = sum + i;
i++; // 循环变量 渐变
}
cout << sum << endl;
// (2) 后置条件循环 do{ } while();
int i = 1;
do {
cout << "我要上天 " << i << " 重" << endl;
i++;
} while (i <= 9);
int j = 0;
do {
cout << "他要入地 " << j << " 层" << endl;
j++;
} while (j < 18);
int i = 1; // (1) 循环变量 初始化
int sum = 0;
do { // (2) 循环条件
sum = sum + i;
i++; // (3) 循环变量 渐变
} while (i <= 100);
cout << sum << endl;
// (3) 计数循环 for (循环变量初始化; 循环条件; 循环变量 渐变) { 代码块 }
for (int i = 1; i <= 9; i++) {
cout << "我要上天 " << i << " 重" << endl;
}
for (int j = 0; j < 18; j++) {
cout << "他要入地 " << j << " 层" << endl;
}
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
cout << sum << endl;
// (4) 无限循环 : while(true) {} , for (;;) {}
int num;
int sum = 0;
while (true) {
cin >> num;
if (num == 0) {
break;
}
// sum = sum + num;
sum += num;
}
cout << sum << endl;
// 无限循环
for (;;) {
}
// (5) 循环语句 :
// continue 跳过当前循环,
// break 停止后续所有循环
for (int i = 1; i <= 18; i++) {
if (i == 4) continue;
if (i == 14) continue;
cout << "他要入地 " << i << " 层" << endl;
}
// (6) 遍历循环
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto num : arr) {
cout << num << " ";
}
cout << endl;
string s = "Hello,World!";
for (auto c : s) {
cout << c << " ";
}
cout << endl;
return 0;
}