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

Why to minimize the use of break and continue in loops in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "Why less break and continue are used in the cycle in C++". In daily operation, I believe that many people have doubts about why they use break and continue as little as possible in the cycle in C++. The editor consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "Why do you use break and continue as little as possible in the cycle of C++?" Next, please follow the editor to study!

ES.77: use break and continue as little as possible in the loop

Reason (reason)

In irregular loops, it is easy to ignore break and continue. There is a significant difference between the break in the loop and the break in the switch statement (you can also put the switch statement in the loop body or the loop in the switch statement. )

Example (sample)

Switch (x) {

Case 1:

While (/ * some condition * /) {

/ /...

Break

} / / Oops! Break switch or break while intended?

Case 2:

/ /...

Break

} Alternative (optional)

Often, a loop that requires a break is a good candidate for a function (algorithm), in which case the break becomes a return.

Loops that require break are usually suitable for making functions (algorithms), where break can be turned into return.

/ / Original code: break inside loop

Void use1 ()

{

Std::vector vec = {/ * initialized with some values * /}

T value

For (const T item: vec) {

If (/ * some condition*/) {

Value = item

Break

}

}

/ * then do something with value * /

}

/ / BETTER: create a function and return inside loop

T search (const std::vector & vec)

{

For (const T & item: vec) {

If (/ * some condition*/) return item

}

Return T (); / / default value

}

Void use2 ()

{

Std::vector vec = {/ * initialized with some values * /}

T value = search (vec)

/ * then do something with value * /

}

Often, a loop that uses continue can equivalently and as clearly be expressed by an if-statement.

In general, loops that use continue can be expressed equivalently and clearly as if statements.

For (int item: vec) {/ / BAD

If (item%2 = = 0) continue

If (item = = 5) continue

If (item > 10) continue

/ * do something with item * /

}

For (int item: vec) {/ / GOOD

If (item%2! = 0 & & item! = 5 & & item

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