【问题标题】:Difference in while and do-while loopswhile 和 do-while 循环的区别
【发布时间】:2018-04-06 03:05:47
【问题描述】:

有人能解释一下使用两种不同类型的 while 循环的目的吗?我是编程新手。如果可能,还提供带有适当 while 循环的示例情况。

我了解如何使用 while 循环。这是我做的:

bool myBool = true;
int i = 0;

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

【问题讨论】:

  • 'do...while' vs. 'while'的可能重复
  • 测试前重复结构与测试后重复结构。你总是想至少做一次吗?真的只是语法糖。
  • @chrisz 不错!这家伙绝对不会保持 100

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


【解决方案1】:

只有在boolean 条件为true 时才会执行while 循环。

    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);

将执行一次。

【讨论】:

    【解决方案2】:

    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循环的流程图

    【讨论】:

      【解决方案3】:

      简单的答案是只有当while语句中的条件为真时才会执行while循环。 不管while语句的条件如何,do 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】:

        while 和 do-while 的区别在于 in

        while (<condition>)
        {
          //statements
        }
        

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

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

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

        【讨论】:

          【解决方案5】:

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

          1. 为什么 C++ 有不同种类的循环? -> 遗留。在C++ 之前的其他语言(尤其是C)有这个功能,所以C++ 选择了它。

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


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

          有很多关于循环的示例和解释的答案,所以我不会在这里重复。我要补充一点,我个人看到的do {} while () 使用最多的是,讽刺的是,不是用于循环,而是用于this

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 2011-04-07
              • 2016-04-01
              • 2015-07-03
              • 2014-10-03
              • 2022-01-03
              • 2016-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多