Posts

Showing posts from February, 2014

#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 a too imaginative way is not a good idea. All parts of the  for  loop should be short