控制结构
条件语句
条件语句允许程序根据不同条件做出决策。
if 语句
基本 if 语句
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "You are an adult." << endl;
}
if (age < 18) {
cout << "You are a minor." << endl;
}
return 0;
}if-else 语句
if-else 示例
#include <iostream>
using namespace std;
int main() {
int score = 85;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
cout << "Grade: B" << endl;
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else if (score >= 60) {
cout << "Grade: D" << endl;
} else {
cout << "Grade: F" << endl;
}
return 0;
}switch 语句
switch 语句在需要将变量与多个值进行比较时很有用。
switch 语句示例
#include <iostream>
using namespace std;
int main() {
char grade = 'B';
switch (grade) {
case 'A':
cout << "Excellent!" << endl;
break;
case 'B':
cout << "Good job!" << endl;
break;
case 'C':
cout << "Well done!" << endl;
break;
case 'D':
cout << "You passed!" << endl;
break;
case 'F':
cout << "Better try again!" << endl;
break;
default:
cout << "Invalid grade!" << endl;
break;
}
return 0;
}重要提示:
- 始终使用 break; 防止穿透行为
- default 情况是可选的,但建议使用
- switch 适用于整数、字符和枚举
循环
循环允许你高效地重复执行代码。
for 循环
for 循环示例
#include <iostream>
using namespace std;
int main() {
// Basic for loop
cout << "Counting from 1 to 5:" << endl;
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
cout << endl;
// Loop with step of 2
cout << "Even numbers from 2 to 10:" << endl;
for (int i = 2; i <= 10; i += 2) {
cout << i << " ";
}
cout << endl;
// Reverse loop
cout << "Countdown from 5 to 1:" << endl;
for (int i = 5; i >= 1; i--) {
cout << i << " ";
}
cout << endl;
return 0;
}while 循环
while 循环示例
#include <iostream>
using namespace std;
int main() {
int number = 1;
cout << "Numbers from 1 to 5 using while:" << endl;
while (number <= 5) {
cout << number << " ";
number++; // Important: increment to avoid infinite loop
}
cout << endl;
// User input validation
int userInput;
cout << "Enter a number between 1 and 10: ";
cin >> userInput;
while (userInput < 1 || userInput > 10) {
cout << "Invalid input! Please enter a number between 1 and 10: ";
cin >> userInput;
}
cout << "Thank you! You entered: " << userInput << endl;
return 0;
}do-while 循环
do-while 循环示例
#include <iostream>
using namespace std;
int main() {
int choice;
do {
cout << "\n=== Menu ===" << endl;
cout << "1. Say Hello" << endl;
cout << "2. Say Goodbye" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Hello there!" << endl;
break;
case 2:
cout << "Goodbye!" << endl;
break;
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
break;
}
} while (choice != 3);
return 0;
}循环控制语句
这些语句允许你控制循环的流程。
break 和 continue
#include <iostream>
using namespace std;
int main() {
cout << "Using break - stop at 7:" << endl;
for (int i = 1; i <= 10; i++) {
if (i == 7) {
break; // Exit the loop when i equals 7
}
cout << i << " ";
}
cout << endl;
cout << "Using continue - skip even numbers:" << endl;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of this iteration
}
cout << i << " ";
}
cout << endl;
// Nested loops with labeled break (using goto - not recommended)
cout << "Finding first pair that sums to 10:" << endl;
bool found = false;
for (int i = 1; i <= 5 && !found; i++) {
for (int j = 1; j <= 5; j++) {
if (i + j == 10) {
cout << "Found: " << i << " + " << j << " = 10" << endl;
found = true;
break; // Break inner loop
}
}
}
return 0;
}嵌套控制结构
你可以将循环和条件语句相互嵌套。
乘法表
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Multiplication Table (1-5):" << endl;
cout << " ";
// Print header
for (int i = 1; i <= 5; i++) {
cout << setw(4) << i;
}
cout << endl;
// Print table
for (int i = 1; i <= 5; i++) {
cout << setw(2) << i << " ";
for (int j = 1; j <= 5; j++) {
cout << setw(4) << (i * j);
}
cout << endl;
}
return 0;
}数字图案
#include <iostream>
using namespace std;
int main() {
int rows = 5;
cout << "Number Triangle:" << endl;
for (int i = 1; i <= rows; i++) {
// Print spaces for alignment
for (int space = 1; space <= rows - i; space++) {
cout << " ";
}
// Print numbers
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}最佳实践
使用有意义的条件
编写清晰、可读的条件来表达意图
避免深度嵌套
使用早期返回或 break 语句来减少嵌套层级
初始化循环变量
始终初始化循环计数器以防止未定义行为
尽可能使用 const
如果循环变量不应改变,将其设为 const