Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How do C++ define loop variables and condition variables

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how C++ defines cyclic variables and condition variables". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ES.6: define loop variables and condition variables within a limited range

Reason (reason)

Readability. Minimize resource usage.

Example (sample)

Void use ()

{

For (string s; cin > > s;)

V.push_back (s)

For (int I = 0; I < 20; + + I) {/ / good: i is local to for-loop

/ /...

}

If (auto pc = dynamic_cast (ps)) {/ / good: pc is local to if-statement

/ /... Deal with Circle...

}

Else {

/ /... Handle error...

}

} Enforcement (implementation recommendations)

Flag loop variables declared before the loop and not used after the loop

Marks a situation in which a loop variable is defined before the loop but is not used after the loop.

(hard) Flag loop variables declared before the loop and used after the loop for an unrelated purpose.

(difficulty) Mark situations in which loop variables are defined before the loop and then used for irrelevant purposes after the loop.

Clear17 and Clearing 20 example (examples of Clearing 17 and Clearing 20)

Note: if,switch and range for initialization statements have also been added to Category 17 and Category 20. The following code needs to be supported by Category 17 and Category 20.

Map mymap

If (auto result = mymap.insert (value); result.second) {

/ / insert succeeded, and result is valid for this block

Use (result.first); / / ok

/ /...

} / / result is destroyed here

Recommendations for the implementation of Category 17 and Category 20 (if you use the Clear17 or Clipper 20 compiler)

Flag selection/loop variables declared before the body and not used after the body

Marks cases where the selection / loop variable is defined before the selection / loop body and is not used after the selection / loop body.

(hard) Flag selection/loop variables declared before the body and used after the body for an unrelated purpose.

(difficulty) Mark situations in which selection / loop variables are defined before the selection / loop body and then used for irrelevant purposes after the selection / loop body.

This is the end of how C++ defines loop variables and condition variables. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report