What?
Code snippets that are repeatedly executed (Are you learning programming on the first day)
Why?
Knowing how to use and when to use each cycle is a decisive factor in creating high-quality software.
How? Checks whether an exit loop at the beginning or end of the loop enters the loop.
- Enter the loop from only one position
- Keep the initialization code on the front of the loop before the flattening.
- Use while (true) to process infinite Loops
- Use more for loops when appropriate
- When the while loop is more suitable, do not use the For Loop
Processing loop body
- Use {} to enclose the loop
- Avoid empty Loop
- Place the cyclic internal operations at the beginning or end of the loop
- Only one thing in a loop
Exit Loop
- The validation cycle can be terminated-simulate it in your mind
- Make the cyclic termination condition apparent
- Do not randomly change the subscript of the for loop to terminate the loop
- Avoid using cyclic subscript outside the loop (dependent on the final value of the subject under the loop)
- Exit with the break in the while loop, instead of the Boolean flag
- Use continue to judge at the beginning of the loop
- If the language supports this function, use a break with a label.
Use cyclic Variables
- Array and cyclic boundary are represented by integers or enumeration types.
- Use a meaningful subscript variable name
- Restrict subscript in this loop
How long should a loop be?
- As short as possible, clear at a glance
- Nested control inLayer 3Less
- Move long loop content to a subroutine
Code complete ch.16 control loop