Code Complete 2: Loops (Part 4)
Tags: codenewbie, codequality, computerscience
Published: 2020-09-23 17:22:26

Loop is an informal term that refers to any kind of iterative control structure—any structure that causes a program to repeatedly execute a block of code. 

Common loop types are for, while, and do-while in C++ and Java, and For-Next, While-Wend, and Do-Loop-While in Microsoft Visual Basic. Go has only one looping construct, the for loop. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p3yqw1sa2ox9nt6l5a84.jpg)

##**Chapter 16: Controlling Loops**
####**Selecting the Kind of Loop**
<ul><li>If you don't know ahead of time exactly how many times you’ll want the loop to iterate, use a while loop.
<li>Don't code a "loop and a half"; instead, loop forever and break in the middle.
<li>Keep for loops simple; if you're explicitly changing the index value, consider a while loop.

**Controlling the Loop**
<ul><li>Put initialization code immediately before the loop.</li>
<li>Use for (;;) or while (true) to write an infinite loop; don't fake it by iterating to a large number.</li>
<li>Reserve the for loop header for initializing the loop, terminating it, and moving toward termination.</li>
<li>Keep statements that control the loop, or move it toward termination, near its beginning or end.</li>
<li>Don't change the index of a for loop to make it terminate.
Avoid using the loop index value after the loop; instead, assign a final value to a variable at the appropriate point inside the loop.</li>
<li>Using a break forces the person reading your code to look inside the loop for an understanding of the loop control.</li>
<li>Inefficient programmers experiment randomly until they find something that works, perhaps replacing a bug with a more subtle one.</li></ul>

##**Chapter 17: Unusual Control Structures**
##**Recursion**
For most situations, recursion produces very complicated solutions that chew up stack space. Use it selectively, and prefer iteration.
For local variables in recursive functions, use new to create objects on the heap as opposed to on the stack.

**Goto**
The use of a goto statement defeats some compiler optimizations, which rely on orderly flow control.
The try-finally construct can sometimes be used to perform the error cleanup that a goto sometimes performs.
Measure the performance of any goto statement used to improve efficiency.

**Key points to note;**
<ul><li>Loops are complicated. Keeping them simple helps readers of your code.</li>
<li>Techniques for keeping loops simple include avoiding exotic kinds of loops, minimizing nesting, making entries and exits clear, and keeping housekeeping code in one place.</li>
<li>Loop indexes are subjected to a great deal of abuse. Name them clearly, and use them for only one purpose.</li>
<li>Think through the loop carefully to verify that it operates normally under each case and terminates under all possible conditions.</li>
<li>Multiple returns can enhance a routine’s readability and maintainability, and they help prevent deeply nested logic. They should, nevertheless, be used carefully.</li>
<li>Recursion provides elegant solutions to a small set of problems. Use it carefully, too.</li>
<li>In a few cases, gotos are the best way to write code that’s readable and maintainable.</li>
<li>Such cases are rare. Use gotos only as a last resort.</li></ul>