在本教程中,我们将借助一些示例来学习 C++ 编程中while和do...while循环的用法。
在计算机编程中,循环用于重复代码块。
例如,假设我们要显示一条消息 100 次。 然后,我们可以使用循环来代替写print语句 100 次。
那只是一个简单的例子; 通过有效利用循环,我们可以在程序中实现更高的效率和复杂性。
C++ 中有 3 种循环。
for循环while循环do...while循环
在上一教程中,我们了解了 C++ for循环。 在这里,我们将学习while和do...while循环。
C++ while循环
while循环的语法为:
while (condition) {// body of the loop}
这里,
while循环求值condition- 如果
condition求值为true,则将执行while循环内的代码。 - 再次求值
condition。 - 该过程一直持续到
condition为false为止。 - 当
condition求值为false时,循环终止。
要了解有关conditions的更多信息,请访问 C++ 关系和逻辑运算符。
While循环流程图

C++ while循环流程图
示例 1:显示从 1 到 5 的数字
// C++ Program to print numbers from 1 to 5#include <iostream>using namespace std;int main() {int i = 1;// while loop from 1 to 5while (i <= 5) {cout << i << " ";++i;}return 0;}
输出
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:仅正数之和
// program to find the sum of positive numbers// if the user enters a negative number, the loop ends// the negative number entered is not added to the sum#include <iostream>using namespace std;int main() {int number;int sum = 0;// take input from the usercout << "Enter a number: ";cin >> number;while (number >= 0) {// add all positive numberssum += number;// take input again if the number is positivecout << "Enter a number: ";cin >> number;}// display the sumcout << "\nThe sum is " << sum << endl;return 0;}
输出
Enter a number: 6Enter a number: 12Enter a number: 7Enter a number: 0Enter a number: -2The sum is 25
在此程序中,提示用户输入一个数字,该数字存储在变量num中。
为了存储数字的总和,我们声明一个变量sum并将其初始化为0的值。
while循环继续,直到用户输入一个负数。 在每次迭代期间,用户输入的数字将添加到sum变量中。
当用户输入负数时,循环终止。 最后,显示总和。
C++ do...while循环
do...while循环是while循环的一种变体,具有一个重要的区别:do...while循环的主体在检查condition之前执行一次。
其语法为:
do {// body of loop;}while (condition);
Here,
- 循环的主体首先执行。 然后求值
condition。 - 如果
condition求值为true,则将再次执行do语句内的循环主体。 - 再次求值
condition。 - 如果
condition求值为true,则将再次执行do语句内的循环主体。 - 该过程一直持续到
condition求值为false为止。 然后循环停止。
do...while循环流程图

C++ do...while循环流程图
示例 3:显示从 1 到 5 的数字
// C++ Program to print numbers from 1 to 5#include <iostream>using namespace std;int main() {int i = 1;// do...while loop from 1 to 5do {cout << i << " ";++i;}while (i <= 5);return 0;}
输出
1 2 3 4 5
Here is how the program works.
| 迭代 | 变量 | i <= 5 |
行为 |
|---|---|---|---|
i = 1 |
未检查 | 打印 1,将i增加到 2 |
|
| 1 | i = 2 |
true |
打印 2 ,将i增加到 3 |
| 2 | i = 3 |
true |
打印 3 ,将i增加到 4 |
| 3 | i = 4 |
true |
打印 4 ,将i增加到 5 |
| 4 | i = 5 |
true |
打印 5 ,将i增大为 6 |
| 5 | i = 6 |
false |
循环结束 |
示例 4:仅正数之和
// program to find the sum of positive numbers// If the user enters a negative number, the loop ends// the negative number entered is not added to the sum#include <iostream>using namespace std;int main() {int number = 0;int sum = 0;do {sum += number;// take input from the usercout << "Enter a number: ";cin >> number;}while (number >= 0);// display the sumcout << "\nThe sum is " << sum << endl;return 0;}
输出 1
Enter a number: 6Enter a number: 12Enter a number: 7Enter a number: 0Enter a number: -2The sum is 25
在此,do...while循环继续进行,直到用户输入一个负数。 当数字为负数时,循环终止;否则为 0。 负数不会添加到sum变量中。
输出 2
Enter a number: -6The sum is 0.
如果用户输入一个负数,则do...while循环的主体仅运行一次。
无限while循环
如果循环的condition始终为true,则循环运行无限次(直到内存已满)。 例如,
// infinite while loopwhile(true) {// body of the loop}
这是无限do...while循环的示例。
// infinite do...while loopint count = 1;do {// body of loop}while(count == 1);
在上述程序中,condition始终为true。 因此,循环体将运行无限次。
for vs while循环
当已知迭代次数时,通常使用for循环。 例如,
// This loop is iterated 5 timesfor (int i = 1; i <=5; ++i) {// body of the loop}
在这里,我们知道for循环将执行 5 次。
但是,while和do...while循环通常在迭代次数未知的情况下使用。 例如,
while (condition) {// body of the loop}
查看以下示例以了解更多信息:
