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

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

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

問題描述

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

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.]

自從聽到這個消息后,我發現這是真的.你怎么看?是否有一個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?

回應一些答案:是的,我理解do/while"和do/while"之間的技術差異.和同時".這是一個涉及循環的可讀性和結構化代碼的更深層次的問題.

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.

讓我換個方式問:假設你被禁止使用do/while"- 是否有一個現實的例子,這會讓你別無選擇,只能使用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:

根據我的經驗,do-statement 是錯誤和混亂的根源.原因是它的主體總是在評估條件之前執行一次.然而,為了讓身體正常工作,即使第一次通過,也必須保持非常類似的情況.比我預想的更多的是,我發現無論是在程序第一次編寫和測試時,還是在其前面的代碼被修改之后,這種情況都不會像預期的那樣保持.我也更喜歡在我可以看到的地方"的條件.因此,我傾向于避免使用 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 循環是 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 循環可以重寫為 while 循環,但是我不同意總是使用 while 循環更好.do while 總是至少運行一次,這是一個非常有用的屬性(最典型的例子是輸入檢查(從鍵盤))

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

這當然可以重寫為 while 循環,但這通常被視為更優雅的解決方案.

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

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

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
How to break out of a loop from inside a switch?(如何從交換機內部跳出循環?)
主站蜘蛛池模板: 日韩精品免费看 | 日韩黄色免费视频 | 久草国产视频 | 成人激情视频在线观看 | 欧美精品一二区 | 成年人免费看片 | 这里都是精品 | 国产aⅴ爽av久久久久成人 | 亚洲黄色大片 | 日韩一区二区在线观看视频 | 亚洲三级黄色片 | 国产免费无遮挡 | 天天操天天操 | 不卡中文字幕 | 天天操夜夜操 | 久久久久久久久久久久久久久久久 | 国产一级特黄aaa大片 | 日本高清网站 | 午夜黄色剧场 | 日韩国产精品一区二区 | 毛片网站免费 | 亚洲一区二区在线播放 | a毛片在线观看 | 亚洲精品中文字幕乱码三区91 | 国产精品免费一区二区三区 | 免费黄色一级 | 成人午夜视频在线观看 | 亚洲免费大片 | 国产福利网 | 一区二区免费看 | 国产香蕉视频 | 中文字幕精品一区久久久久 | 正在播放国产精品 | 午夜激情网站 | 久操视频在线 | 蜜桃成人在线 | 欧美日韩精品 | 97视频在线观看免费 | 91视频一区二区三区 | 成人免费公开视频 | 久久性色 |