while和do-while循环的区别

时间:2018-04-06 03:05:47

标签: c++ loops while-loop do-while

有人可以解释两种不同类型的while循环的目的吗?我是编程新手。如果可能的话,还提供带有正确while循环的示例情况。

我理解如何使用while循环。这就是我所做的:

bool myBool = true;
int i = 0;

while (myBool) {
  if (i > 10) {
      myBool = false;
  }
  i = i + 1;
}

7 个答案:

答案 0 :(得分:9)

while循环仅在boolean条件为true时执行。

    while (true) {
        // INSERT CODE HERE
        std::cout << "boolean condition is true - inside my while loop";
    }

在循环执行一次后,do while检查boolean条件。

    do {
        // INSERT CODE HERE
        std::cout << "inside my while loop regardless of boolean condition";
    } while (true);

明确地说:do while循环保证至少执行一次,而while循环根本不能保证执行。

类似地,

while (false) {
    // INSERT CODE HERE
    std::cout << "this will never execute";
}

将永远不会执行和

do {
    // INSERT CODE HERE
    std::cout << "this will execute only once";
} while (false);

将执行一次。

答案 1 :(得分:5)

基本

whiledo-while之间最重要的区别在于do-While,代码块至少执行一次;即,do-while循环至少运行一次,即使给出的条件为假。这意味着while 条目控制do-while 退出控制

以不同的方式表达:

  • while - 您的条件是在循环块的开头,然后生成 可能永远不会进入循环。
  • do while - 您的条件位于循环块的末尾,然后生成 必须至少进入一次循环。

亲眼看看:

使用while:

 int n=1;
 while(n<1)
    std::cout << "This does not get printed" << endl;

使用do-while:

int n=1;
do
   std::cout << "This one gets printed" << endl;
while(n<1);

绩效衡量指标

如果编译器不能胜任优化,那么

do-while会更好。 do-while只有一个条件跳转,而forwhile则有条件跳转和无条件跳转。对于流水线并且不进行分支预测的CPU,这可以对紧密循环的性能产生很大的影响。

此外,由于大多数编译器都足够智能来执行此优化,因此反编译代码中的所有循环通常都是do-while(如果反编译器甚至无法从后向本地gotos重构循环)。

语法

while

while(condition is true)
{ 
       something
}

do=while

do
{
      something
}while(condition is true)

工作示例

while循环

#include <iostream>

int main() 
{
    int number, i = 1, factorial = 1;

    std::cout << "Enter a positive integer: ";
    std::cin >> number;

    while ( i <= number) {
        factorial *= i;      //factorial = factorial * i;
        ++i;
    }

    std::cout<<"Factorial of "<< number <<" = "<< factorial;
    return 0;
}

输出:

  

输入正整数:4
  因子4 = 24

流速:

  • 最初,i = 1,测试表达式i <=数字为真且是阶乘的 变成1。
  • 变量i更新为2,测试表达式为true,factorial 变成2。
  • 变量i更新为3,测试表达式为true,factorial 变成了6。
  • 变量i更新为4,测试表达式为true,factorial 变成了24岁。
  • 变量i更新为5,测试表达式为false且while循环 终止。

do-while循环

#include <iostream>

int main()
{
    double number, sum = 0;

    // loop body is executed at least once
    do
    {
        std::cout << "Enter a number: ";
        std::cin >> number;
        sum += number;
    }
    while(number != 0.0);

    std::cout << "Sum = " << sum;

    return 0;
}

答案 2 :(得分:4)

  

do while循环是控制流语句,它们至少执行一次代码块,然后循环的迭代取决于在循环底部检查的条件,它们最好在你想要至少一次循环执行,对于前

#include <stdio.h>

int main () {


   int c = 50;

   /* The do will be executed */
   do {
      printf("value of c: %d\n", c);
      c = c + 1;
   }while( c < 20 );//It will depend on the condition
 printf("any string");
   return 0;
}

以下是do while循环的流程图 enter image description here

答案 3 :(得分:2)

简单的答案是while循环只有在while语句中的条件为真时才会执行。 无论while语句条件如何,while循环都会执行一次。

#include <iostream>
using namespace std;

int main(int argc, char *argv[]){
    int i = 1;
    while( i < 1){     // this loop will never execute as 1 is not smaller then 1
       i++;            // if the loop was working we would get print 2 here  
       cout << i << endl;
    }

    cout << i << endl; // this one will print outside of loop value 1

    do{
       i++;            // increase i to 2
       cout << i << endl;  // this will print 2
    } while (i < 1);   // This loop will execute at least once and as the condition of 2 is not smaller then 1 it will exit after one execution 
return 0;
}

答案 4 :(得分:2)

while和do-while之间的区别在于

while (<condition>)
{
  //statements
}

我们可以通过使用测试条件来控制是否进入循环。 而在

do
{
 //statements
} while (<condition>);

代码必须至少进入一次循环才能通过使用条件退出。 因此,如果我们想要至少进入一次循环,我们应该使用do-while而如果我们想测试并决定是否进入循环,我们必须使用while。

答案 5 :(得分:1)

明确回答您的第一个问题:

  1. 为什么C ++有不同类型的循环? - &gt;遗产。 C之前的其他语言(特别是C++)具有此功能,因此C++选择了此功能。

  2. 为什么其他语言有它? - &gt;这变得很混乱,但一个很好的解释是早期语言通常没有优化编译器,所以你的代码直接映射到机器代码。提供各种循环语法允许程序员编写结构化代码,仍可为其特定情况生成良好的机器代码。

  3. 在实践中,很少看到真正的do {} while ()循环。这可能是因为for(或基于范围的for)和while () {}具有比do {} while ()更强大的功能:每个循环都可以进行无条件的第一次循环迭代,但是反向是不正确的。在迭代(可能是空的)序列的非常常见的情况下,像do {} while ()这样的保证循环体执行实际上是错误的。

    关于循环的例子和解释有很多答案,所以我不打算在这里重复这个。我会补充说,我亲眼看到do {} while ()使用的最多,但具有讽刺意味的是,不是用于循环,而是this

答案 6 :(得分:1)

do-while循环在while()中的条件之前执行任务。它适用于您尝试为每个错误操作(即用户身份验证,错误的菜单项)提示相同内容的情况。另一方面,一个简单的while循环执行直到条件为真(注意:在大多数情况下,人们使用for循环而不是while来定义计数器,初始化,设置条件和增量/减量 - 全部在同一行中)。

相关问题