-
Indenting loops
-
It's easy (mostly) to learn how to indent loops. For for
and while loops, just indent like if statements.
It's just the same!
For do while, I suggest a different style.
-
do while
-
I believe you should use blocks for do-while loops.
The problem with do-while is that it looks like a
while loop. For example, look at:
do
i++ ;
while ( i < 10 ) ;
If you're not paying attention, you see the while ( i < 10 ) ;
which looks very strange. It takes a moment to realize you're not
looking at a while loop, you're looking at a do while
loop.
To cut down on the time it takes to recognize a do while loop,
I suggest using braces, as in:
do {
i++ ;
} while ( i < 10 ) ;
At this point, you see } while ( i < 10 ). The close
brace at the beginning makes you think "Ah, that must be the end of
a do while loop".
-
Summary
-
Once you understand how to indent if statements and all
of its variations, it's quite easy to indent loops, since they
are indented the same way. If anything, it's easier, because you
don't have the same complications as indenting if-else
statements.