Using the g++ compiler with a for loop like what is shown above produces an optimisation warning (provided you have all the warnings enabled: -Wall -Wextra -pedantic), because:
for (int a = 0; a < 10; a++);
is the same as:
int a =10;
Null statements are sometimes valid in while loops, it seems the best practice (after some discussion previously) might be to do this:
1 2 3
|
while (condition){
; //null statement
}
| |
The braces & particularly the comment show that it is intentional. I, and many others, always use braces even for single statements because it can save you one day when you add more code.
The following code should really be an error (unfortunately it isn't, maybe the warning above is enough) :
for(;;);
because it produces an infinite loop that can never be broken out of.
I do like the use of the for version of an infinite loop (when the use of infinite loop is valid), because it will always work regardless of whether you are using C or C++, or any other C-like language.
With any compound statement, I like the idea of putting the opening brace immediately after any condition, to discourage the misuse of the pesky semicolon. Although I observe that newbies are capable of anything, including the semicolon & an opening brace together.
I also dislike single statements on the same line as the loop or conditional statement, unless it is a simple return statement in an explicit in-line function. But that is my preference though.
Hope all goes well.