Code Book 2 Reading Notes 09-Chapter 16 controlling Loops

Source: Internet
Author: User

Chapter 16 controlling Loops
Control Loop

"Loop" is an informal term used to refer to any iterative control structure-any one that can cause applicationsProgramRepeated execution

Line SegmentCode.

16.1 selecting the kind of Loop
Select the type of Loop
· The Count loop is executed once for each employee.
· Continuous evaluate loop (continuously evaluation loop)
It does not know how many times it will be executed in advance. It will check whether it should end at each iteration.
· Once an endless loop is started, it will continue to be executed. You will find it in embedded systems such as pacemakers, microwave ovens, and navigators.
· The iterator loop performs an operation on each element in the container class.

· When to use a 'with' loop when to use 'with' Loop
1. Loop with test at the beginning of the Beginning Detection
2. Loop with test at the end of the loop

· When to use a loop-with-exit Loop
1. Normal loop-with-exit loops normal loop with exit
The loop with exit is also a structured control structure for single-in-one and single-out, and is also the first choice for loop control. It turns out that he is easier to handle than other types of loops.

.
2. Abnormal loop-with-exit loops abnormal loop with exit
That is, the cycle of exit using break.

· When to use A' for 'loop when to use for Loop
If you need a loop with a fixed number of executions, the for loop is a good choice. You can use the for loop to execute simple operations that do not require internal loop control.

. If there is a condition that requires the execution to jump out of the loop, you should use the while loop instead.
Do not force the for loop to terminate by modifying the lower mark value. In this case, use the while loop instead. The for loop is for simple purposes,

It is best to use a while loop to process more complex loops.

· When to use a 'foreach' Loop
Foreach loops or their equivalents (foreach in C #, for-each in VB, and for-in Python) are suitable

. Its advantage is that it eliminates loop internal processing arithmetic. This eliminates any loop control.AlgorithmThe possibility of errors.

16.2 controlling the loop control
· Entering the loop into the loop
1. Enter the loop from only one position
2. Keep the initialization code before the loop.
3. Use while (true) to indicate an infinite loop
Some programmers tend to use for (;), which is acceptable.
4. Use more for loops when appropriate
In a for loop, all the relevant code is written at the top of the loop, so it is easier to modify it. If you can replace it with a for loop

This is the type of loop.

· Processing the middle of the loop to process the loop body
1. Use '{' and '}' to enclose the statements in the loop-Defensive Programming practices
2. Avoid empty loops
3. Put the cyclic internal operations either at the beginning of the loop or at the end of the loop
4. Only one thing in a loop

· Exiting the loop exit Loop
1. Try to confirm that the cycle can be terminated
2. Make the cyclic termination condition look obvious.
3. Do not randomly change the subscript of the for loop to terminate the loop.
You can use a while loop to obtain more control over exit conditions.
4. Avoid code dependent on the final value of the loop subscript.
After the loop ends, it is difficult to use the loop subscript.
5. Consider using security counters
A Security Counter is a variable that increments after each loop to determine whether the number of executions of the loop is too large.
However, security counters do not cover all diseases. Every time you use a Security Counter in the code, it also increases complexity and may cause other errors.

.
· Exiting loops early exit Loop
Many languages provide a method to terminate a loop except when the for or while termination conditions are met. For example, break and continue.
1. Use the break statement in the while loop instead of the Boolean flag.
Adding the bool mark to the while loop to exit the loop body may make the loop hard to understand. Use break to replace a series of if detection, sometimes

You can remove some indent layers from the loop to simplify loop control.
2. Be careful with the loops where many breaks are distributed.
Using multiple breaks is not necessarily an error, but if they appear in a loop, it is a warning mark. It is like a canary in a coal mine.

Singing brightly consumes oxygen.
3. Use continue at the beginning of the loop to determine
One good way to use continue is to let the Code go beyond the rest of the loop body after the condition judgment is completed at the beginning of the loop body.
Using continue in this way can avoid using an if statement that can indent the entire loop body. If the continue appears in the center or tail of the loop,

So we should use if instead.
4. If the language supports this function, use the break structure with a label (JAVA supports the use of the break statement with a label (labeled break ).)
Example: Break call_center_down;
5. Be careful when using break and continue
Do not use break unless you have already considered various replacement solutions. We cannot determine whether continue and break are good or bad. In fact, the suggestions here are very good.

Simple: if you cannot prove the legitimacy of using break or continue, do not use them.

· Checking endpoints check the endpoint
For a simple loop, you usually need to pay attention to three situations: the initial situation, the intermediate situation of any choice, and the final situation.
You can benefit a lot by simulating and performing manual operations in your mind. This kind of intellectual training brings about the following benefits: fewer mistakes in the initial encoding phase

To quickly identify errors and better understand the application as a whole.

· Using loop varables use cyclic Variables
1. An integer or enumeration type is used to represent the boundary between arrays and loops.
2. Use meaningful variable names in nested loops to improve readability
If you use a two-or multi-dimensional array, you should use meaningful subscript names to clarify your intention.

3. Avoid loop subscripts with meaningful names.
Habitual use of I, j, and K may lead to subscripts in a string (cross-talk)
4. restrict the scope of the circular subscript variable to this loop.

· How long shocould a loop be cycle should be long
1. The cycle should be as short as possible to be clear at a glance.
2. Limit nesting to three layers.
Research shows that when nesting exceeds three layers, the programmer's understanding of the loop is greatly reduced (Yourdon 1986a ).
3. Move the content of a long loop to a subroutine
4. Clear long cycles.

16.3 creating loops easily-from the inside out easily creates a loop-from inside out

16.4 corresponsponbetween loops and arrays relationship between loops and Arrays
Loops are closely related to arrays. In many cases, loops are used to manipulate arrays. In addition, the cyclic counter corresponds to the array subscript one by one.

Key Points of keypoints
· The cycle is complex. Keeping the loop simple will help others read your code.
· Tips for keeping loops simple: Avoid using weird loops, reduce nested hierarchies, make portals and exits clear, and place internal operation code in one place.
· Cyclic subscript is easily abused. Therefore, the names must be accurate and each of them must be used for only one purpose.
· Carefully consider the loop to confirm that it runs normally in every situation and exits under all possible conditions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.