久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是否需要“do {...} while ()"?環(huán)形?

Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
本文介紹了是否需要“do {...} while ()"?環(huán)形?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

Bjarne Stroustrup(C++ 創(chuàng)造者)曾經(jīng)說過他避免do/while"循環(huán),并且更喜歡根據(jù)while"來編寫代碼.循環(huán)代替.[見下面的引用.]

Bjarne Stroustrup (C++ creator) once said that he avoids "do/while" loops, and prefers to write the code in terms of a "while" loop instead. [See quote below.]

自從聽到這個(gè)消息后,我發(fā)現(xiàn)這是真的.你怎么看?是否有一個(gè)do/while"的例子?比使用while"更清晰、更容易理解.取而代之?

Since hearing this, I have found this to be true. What are your thoughts? Is there an example where a "do/while" is much cleaner and easier to understand than if you used a "while" instead?

回應(yīng)一些答案:是的,我理解do/while"和do/while"之間的技術(shù)差異.和同時(shí)".這是一個(gè)涉及循環(huán)的可讀性和結(jié)構(gòu)化代碼的更深層次的問題.

In response to some of the answers: yes, I understand the technical difference between "do/while" and "while". This is a deeper question about readability and structuring code involving loops.

讓我換個(gè)方式問:假設(shè)你被禁止使用do/while"- 是否有一個(gè)現(xiàn)實(shí)的例子,這會(huì)讓你別無選擇,只能使用while"編寫不干凈的代碼?

Let me ask another way: suppose you were forbidden from using "do/while" - is there a realistic example where this would give you no choice but to write unclean code using "while"?

來自C++ 編程語言",6.3.3:

From "The C++ Programming Language", 6.3.3:

根據(jù)我的經(jīng)驗(yàn),do-statement 是錯(cuò)誤和混亂的根源.原因是它的主體總是在評(píng)估條件之前執(zhí)行一次.然而,為了讓身體正常工作,即使第一次通過,也必須保持非常類似的情況.比我預(yù)想的更多的是,我發(fā)現(xiàn)無論是在程序第一次編寫和測試時(shí),還是在其前面的代碼被修改之后,這種情況都不會(huì)像預(yù)期的那樣保持.我也更喜歡在我可以看到的地方"的條件.因此,我傾向于避免使用 do 語句. -Bjarne

In my experience, the do-statement is a source of errors and confusion. The reason is that its body is always executed once before the condition is evaluated. However, for the body to work correctly, something very much like the condition must hold even the first time through. More often than I would have guessed, I have found that condition not to hold as expected either when the program was first written and tested, or later after the code preceding it has been modified. I also prefer the condition "up front where I can see it." Consequently, I tend to avoid do-statements. -Bjarne

避免 do/while 循環(huán)是 C++ 核心指南中的建議 作為 ES.75,避免do 語句.

Avoiding the do/while loop is a recommendation included in the C++ Core Guidelines as ES.75, avoid do-statements.

推薦答案

是的,我同意 do while 循環(huán)可以重寫為 while 循環(huán),但是我不同意總是使用 while 循環(huán)更好.do while 總是至少運(yùn)行一次,這是一個(gè)非常有用的屬性(最典型的例子是輸入檢查(從鍵盤))

Yes I agree that do while loops can be rewritten to a while loop, however I disagree that always using a while loop is better. do while always get run at least once and that is a very useful property (most typical example being input checking (from keyboard))

#include <stdio.h>

int main() {
    char c;

    do {
        printf("enter a number");
        scanf("%c", &c);

    } while (c < '0' ||  c > '9'); 
}

這當(dāng)然可以重寫為 while 循環(huán),但這通常被視為更優(yōu)雅的解決方案.

This can of course be rewritten to a while loop, but this is usually viewed as a much more elegant solution.

這篇關(guān)于是否需要“do {...} while ()"?環(huán)形?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
How to break out of a loop from inside a switch?(如何從交換機(jī)內(nèi)部跳出循環(huán)?)
主站蜘蛛池模板: 日韩成人在线看 | 亚洲精品一 | 亚洲三区视频 | 偷拍自拍在线观看 | 亚洲激情一区二区三区 | 可以看黄的视频 | 国产色在线| 91麻豆精品国产91久久久更新资源速度超快 | 欧美性受xxxx | 天久久 | 亚洲午夜网 | 日日摸夜夜爽人人添av | 国产色在线 | 91一区 | 韩日av在线| 精品中文字幕久久 | 日韩精品一区二区三区中文在线 | 欧美一级免费 | 伊人免费视频二 | 国产成人免费视频网站高清观看视频 | 久久久久久久久中文字幕 | 成人精品一区二区三区 | 欧美亚洲综合久久 | 亚洲高清在线 | 999久久久久久久久6666 | 天天综合干 | 日韩一区二区免费视频 | 久久久一区二区三区四区 | 一区二区亚洲 | 少妇黄色 | 欧美一级大片 | 精品欧美一区二区三区精品久久 | 亚洲成人免费视频在线观看 | 国产精品免费av | 亚洲国产精品一区在线观看 | 亚洲91视频 | 日韩精品视频在线 | 国产精品99久久久久久久久久久久 | 成人小视频在线观看 | 日本高清视频在线播放 | h视频免费在线观看 |