NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
# C++ `for`循环 > 原文: [https://www.programiz.com/cpp-programming/for-loop](https://www.programiz.com/cpp-programming/for-loop) #### 在本教程中,我们将在一些示例的帮助下了解 C++ `for`循环及其工作原理。 在计算机编程中,循环用于重复代码块。 例如,假设我们要显示一条消息 100 次。 然后,我们可以使用循环来代替写`print`语句 100 次。 那只是一个简单的例子; 通过有效地使用循环,我们可以在程序中实现更高的效率和复杂性。 C++ 中有 3 种循环类型。 * `for`循环 * `while`循环 * `do...while`循环 本教程重点介绍 C++ `for`循环。 我们将在以后的教程中学习其他类型的循环。 * * * ## C++ `for`循环 `for`循环的语法为: ```cpp for (initialization; condition; update) { // body of-loop } ``` 这里, * `initialization` - 初始化变量,仅执行一次 * `condition` - 如果执行`true`,则执行`for`循环的主体 如果执行`false`,则终止`for`循环 * `update` - 更新初始化变量的值,然后再次检查条件 要了解有关`conditions`的更多信息,请查看我们的 [C++ 关系和逻辑运算符](/cpp-programming/relational-logical-operators)教程。 * * * ## C++ 中`for`循环的流程图 ![C++ for loop flowchart](https://img.kancloud.cn/f3/d9/f3d9c3f30adbf3e28b8537d1eb06d483.png "C++ for loop flowchart") C++ 中`for`循环的流程图 * * * ### 示例 1:从 1 到 5 打印数字 ```cpp #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << i << " "; } return 0; } ``` **输出** ```cpp 1 2 3 4 5 ``` 该程序的工作原理如下 | 迭代 | 变量 | `i <= 5` | 行为 | | --- | --- | --- | --- | | 1 | `i = 1` | `true` | 1 被打印。`i`增加到`2`。 | | 2 | `i = 2` | `true` | 2 被打印。`i`增加到`3`。 | | 3 | `i = 3` | `true` | 3 被打印。`i`增加到`4`。 | | 4 | `i = 4` | `true` | 4 被打印。`i`增加到`5`。 | | 5 | `i = 5` | `true` | 5 被打印。`i`增加到`6`。 | | 6 | `i = 6` | `false` | 循环终止 | * * * ### 示例 2:显示文本 5 次 ```cpp // C++ Program to display a text 5 times #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << "Hello World! " << endl; } return 0; } ``` **输出** ```cpp Hello World! Hello World! Hello World! Hello World! Hello World! ``` 该程序的工作原理如下 | 迭代 | 变量 | `i <= 5` | 行为 | | --- | --- | --- | --- | | 1 | `i = 1` | `true` | 打印`Hello World!`并将`i`增加到`2`。 | | 2 | `i = 2` | `true` | 打印`Hello World!`并将`i`增加到`3`。 | | 3 | `i = 3` | `true` | 打印`Hello World!`并将`i`增加到`4`。 | | 4 | `i = 4` | `true` | 打印`Hello World!`并将`i`增加到`5`。 | | 5 | `i = 5` | `true` | 打印`Hello World!`并将`i`增加到`6`。 | | 6 | `i = 6` | `false` | 循环终止 | * * * ### 示例 3:查找前 n 个自然数的总和 ```cpp // C++ program to find the sum of first n natural numbers // positive integers such as 1,2,3,...n are known as natural numbers #include <iostream> using namespace std; int main() { int num, sum; sum = 0; cout << "Enter a positive integer: "; cin >> num; for (int count = 1; count <= num; ++count) { sum += count; } cout << "Sum = " << sum << endl; return 0; } ``` **输出** ```cpp Enter a positive integer: 10 Sum = 55 ``` 在上面的示例中,我们有两个变量`num`和`sum`。`sum`变量分配有`0`,`num`变量分配有用户提供的值。 请注意,我们使用了`for`循环。 ```cpp for(int count = 1; count <= num; ++count) ``` Here, * `int count = 1`:初始化`count`变量 * `count <= num`:只要`count`小于或等于`num`,就运行循环 * `++count`:每次迭代将`count`变量增加 1 当`count`变为`11`时,`condition`为`false`,并且`sum`等于`0 + 1 + 2 + ... + 10`。 * * * ## 基于范围的`for`循环 在 C++ 11 中,引入了一个新的基于范围的`for`循环来处理诸如**数组**和**向量**之类的集合。 其语法为: ```cpp for (variable : collection) { // body of loop } ``` 在此,对于`collection`中的每个值,都会执行`for`循环,并将该值分配给`var`。 * * * ### 示例 4:基于范围的循环 ```cpp #include <iostream> using namespace std; int main() { int num_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int n : num_array) { cout << n << " "; } return 0; } ``` **输出** ```cpp 1 2 3 4 5 6 7 8 9 10 ``` 在上面的程序中,我们声明并初始化了一个名为`num_array`的`int`数组。 它有 10 个项目。 在这里,我们使用了基于范围的`for`循环来访问数组中的所有项目。 * * * ### C++ 无限循环 如果`for`循环中的`condition`始终为`true`,则它将永远运行(直到内存已满)。 例如, ```cpp // infinite for loop for(int i = 1; i > 0; i++) { // block of code } ``` 在上面的程序中,`condition`始终为`true`,它将无限次运行代码。 * * * 查看以下示例以了解更多信息: * [C++ 程序:计算自然数的总和](/cpp-programming/examples/sum-natural-number) * [C++ 程序:查找阶乘](/cpp-programming/examples/factorial) * [C++ 程序:生成乘法表](/cpp-programming/examples/multiplication-table) * * * 在下一个教程中,我们将学习`while`和`do...while`循环。