4.13 Loops
Put spaces in between mathmatical operators as if they are alone.
Put spaces after semicolons.
EXAMPLE: BAD:
for(int i=0;i<100;i++){
//code
}
GOOD:
for(int i = 0; i < 100; ++i) {
//code
}
In a for
loop, instead of i++
, try ++i
. The latter has one less operation.
EXCEPTION: Whether the ++
goes before or after the variable affects operator precedence. If the post fix version (i++
) is required for order of operations purpose, use it.
Prefer while
loops to do...while
. The former is easier to understand at a glance.
EXAMPLE: BAD:
for(int i = 0; i < 3; i++) {
//code
}
GOOD:
for(int i = 0; i < 3; ++i) {
//code
}