Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

C++ has obvious loop variables in why the for statement is better than the while statement.

Shulou Source: shulou.com Published: 2022-06-01 19:30:06 09月18日 Update

This article mainly explains the "C++ there are obvious cyclic variables in why the for sentence is better than the while sentence", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "C++ there are obvious cyclic variables in why the for sentence is better than the while sentence" it!

ES.72: for statements are better than while statements if there are obvious loop variables

Reason (reason)

Readability: the complete logic of the loop is visible "up front". The scope of the loop variable can be limited.

Readability: all the logic of the loop can be seen directly in the foreground. The scope of a loop variable can be limited to a loop.

Example (sample)

For (gsl::index I = 0; I < vec.size (); iTunes +) {

/ / do work

}

Example, bad (negative example)

Int I = 0

While (I < vec.size ()) {

/ / do work

ITunes +

}

Thank you for reading, the above is "C++ has obvious loop variables in why the for statement is better than the while statement", after the study of this article, I believe you have a deeper understanding of why the for statement is better than the while statement in the obvious loop variable in C++, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Sentences loops variables obvious better better learning content examples functions foreground reasons negative readability that is ideas situations articles more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei NVidia Docker Redmi