Posts

Showing posts with the label loops

#10. Choose the right loop structure

C++ has three loop instructions: for , while and do ... while . To choose wisely among them, you need to know what their differences are. 1) for A  for  loop makes sense when you repeat an action for a known number of times, or for all the elements of a known set. This is the syntax of a  for   loop: for (initialization ; condition; expression) {     statement } statement  is actually a compound statement, that is, a block of code. This is called the body of the loop. In contrast, the initialization, condition and expression together are called the header of the loop. The initialization is a single statement which ends at the semicolon I wrote after it. The condition is a boolean expression. The expression is a statement. It should be a single statement which modifies one variable which is involved in the condition (see #guideline #5 ). The initialization is performed. Then, the condition is evaluated. Then, two things can happen. I...

#6. Ensure entry and exit conditions in loops

There are at least two questions you should always ask yourself while you're writing code for a loop - and ask yourself again after writing it. Here they are: Will the loop body be entered at least once? Will the loop ever be exited? If your loop is a do .. while , the first question has an obvious answer: yes. It will be entered at least once (as long as the execution point reaches it, of course). That's why you chose that structure in the first place. If it is a while or a for , you should pay more attention to that. Review the loop condition thoroughly and think about the possibility of it being false at the very beginning of the loop. In that case, the loop body would not be executed. Is that scenario correct your design, or is it something to avoid? If the latter is true, then you should write the specific code to handle that. About the second question, the loop will be exited whenever its condition is evaluated to false . The condition is evaluated once for each it...

#5. "for" loops should be simple and well-formed

for loops should be well-formed. This means: They should have a single loop counter, which shall be of integral type or an iterator. The loop counter should only be modified in the increment expression of the loop. The loop counter should be incremented or decremented by the same amount at each loop iteration. The loop counter should be accompanied in the loop condition, if anything, by boolean variables. The loop body should be a compound statement, delimited by brackets. The loop body should not contain labels which are the destination of goto  statements. The loop body should contain at most one break statement. Use continue with care, preferably in the beginning of the loop body, to exclude certain iterations from the action. A  for  loop is a practical, readable (once you get used to it) and terse construct, but you need to use it well. Because of its uncommon syntax, using it in ...