Skip to content

Latest commit

 

History

History
337 lines (227 loc) · 7.03 KB

File metadata and controls

337 lines (227 loc) · 7.03 KB

C++ whiledo...while循环

原文: https://www.programiz.com/cpp-programming/do-while-loop

在本教程中,我们将借助一些示例来学习 C++ 编程中whiledo...while循环的用法。

在计算机编程中,循环用于重复代码块。

例如,假设我们要显示一条消息 100 次。 然后,我们可以使用循环来代替写print语句 100 次。

那只是一个简单的例子; 通过有效利用循环,我们可以在程序中实现更高的效率和复杂性。

C++ 中有 3 种循环。

  1. for循环
  2. while循环
  3. do...while循环

在上一教程中,我们了解了 C++ for循环。 在这里,我们将学习whiledo...while循环。


C++ while循环

while循环的语法为:

while (condition) {
    // body of the loop
}

这里,

  • while循环求值condition
  • 如果condition求值为true,则将执行while循环内的代码。
  • 再次求值condition
  • 该过程一直持续到conditionfalse为止。
  • condition求值为false时,循环终止。

要了解有关conditions的更多信息,请访问 C++ 关系和逻辑运算符


While循环流程图

C++ while loop flowchart

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 5
    while (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 user
    cout << "Enter a number: ";
    cin >> number;

    while (number >= 0) {
        // add all positive numbers
        sum += number;

        // take input again if the number is positive
        cout << "Enter a number: ";
        cin >> number;
    }

    // display the sum
    cout << "\nThe sum is " << sum << endl;

    return 0;
}

输出

Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2

The 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 loop flowchart

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 5
    do {
        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 user
        cout << "Enter a number: ";
        cin >> number;
    }
    while (number >= 0);

    // display the sum
    cout << "\nThe sum is " << sum << endl;

    return 0;
}

输出 1

Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2

The sum is 25

在此,do...while循环继续进行,直到用户输入一个负数。 当数字为负数时,循环终止;否则为 0。 负数不会添加到sum变量中。

输出 2

Enter a number: -6
The sum is 0.

如果用户输入一个负数,则do...while循环的主体仅运行一次。


无限while循环

如果循环的condition始终为true,则循环运行无限次(直到内存已满)。 例如,

// infinite while loop
while(true) {
    // body of the loop
}

这是无限do...while循环的示例。

// infinite do...while loop

int count = 1;

do {
   // body of loop
} 
while(count == 1);

在上述程序中,condition始终为true。 因此,循环体将运行无限次。


for vs while循环

当已知迭代次数时,通常使用for循环。 例如,

// This loop is iterated 5 times
for (int i = 1; i <=5; ++i) {
   // body of the loop
}

在这里,我们知道for循环将执行 5 次。

但是,whiledo...while循环通常在迭代次数未知的情况下使用。 例如,

while (condition) {
    // body of the loop
}

查看以下示例以了解更多信息: