之间&QUOT差异;而"环和"做好而"循环 [英] Difference between "while" loop and "do while" loop

查看:197
本文介绍了之间&QUOT差异;而"环和"做好而"循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是while循环之间的差异,while循环做。我曾经认为这两个是完全same.Then我碰到下面的一段code的传来:

What is the difference between while loop and do while loop. I used to think both are completely same.Then I came across following piece of code :

do {
        printf("Word length... ");
        scanf("%d", &wdlen);
    } while(wdlen<2);

这code完美的作品。它打印字长和tascans输入。但是,当我把它改成

This code works perfectly. It prints word length and tascans the input. But when I changed it to

while(wdlen<2){
        printf("Word length... ");
        scanf("%d", &wdlen);
    } 

它给出​​了一个空白屏幕。它不工作。因此,有两个回路之间存在一些功能上的差异。任何人都可以解释一下吗?先谢谢了。

It gives a blank screen. It do not work. So there is some functional difference between both loops. Can anybody explain it? Thanks in advance.

编辑:有没有在这两个其他任何区别

EDIT : Is there any other difference in these two ?

推荐答案

做的,而一次循环检查,同时的条件之前执行循环的内容。

The do while loop executes the content of the loop once before checking the condition of the while.

而A ,而循环将执行内容之前先检查条件。

Whereas a while loop will check the condition first before executing the content.

在这种情况下会等待用户输入与scanf()的,它绝不会在执行,而环路wdlen没有初始化,可能只包含这可能是一个垃圾值大于2

In this case you are waiting for user input with scanf(), which will never execute in the while loop as wdlen is not initialized and may just contain a garbage value which may be greater than 2.

这篇关于之间&QUOT差异;而&QUOT;环和&QUOT;做好而&QUOT;循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆