For loop ending with semi colon

This following loop is correct:
for (; ;);

I thought the rule was that you can't put a semi colon after a for loop? Can someone explain why this is correct, what happens when you add a semi colon after a for loop, and when we should use the semi colon after a for loop. thanks
Yes that loop is syntactically correct. You are allowed to have a semicolon after the loop.

what happens when you add a semi colon after a for loop

The semicolon signifies that there is no body of the loop. In the above loop you have created an endless
loop that actually does nothing.

when we should use the semi colon after a for loop.

In my opinion you should never use a semicolon after a loop, instead use braces to signify an empty loop body.

1
2
for (; ;)
{ /* Empty body */ }


A semi-colon in this location translates to an empty statement.

The following are the same:

1
2
3
4
5
for (int a = 0; a < 10; ++a);

for (int a = 0; a < 10; ++a)
{
}


They both iterate ten times but do nothing because their body is empty.

You can do it with other constructs, too, and it leads to much confusion.
The following does NOT do what it seems at first glance.

1
2
3
4
5
if (a == 10); // Does nothing if a equals ten - empty body to the 'if'
{
  // And this is ALWAYS executed
  cout << "A is ten" << endl;
}


It's the same as writing
1
2
3
4
5
6
7
if (a == 10)
{
}

{
  cout << "A is ten" << endl;
}


So you shouldn't put trailing semi-colons as it's confusing (but legal!)

Jim
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.
Last edited on
Topic archived. No new replies allowed.