Mr Guliarte
Mr Guliarte

Reputation: 739

Does the "for" loop in C require a "{}" after it?

I had this code in my program:

if (primeiro != atual){                        
                    for (i = 0; i < atual -> numeroChaves; i++) 
                    // comment
}

And I was receiving the following error:

d8641900: In function 'printaArvore':
d8641900:130:7: error: expected expression before '}' token
       }
       ^     

So I made the following change in the code:

if (primeiro != atual){                        
                        for (i = 0; i < atual -> numeroChaves; i++){}
                        // comment
    }

and it runned smoothly.

My doubt is: is it some problem with my code, or it is a rule applied in all cases?

The funny part is that, in other part of my code, I have a similar situation (no "{}" after the for loop), but after it I have a line with a valid command, and it runs perfectly.

Upvotes: 2

Views: 171

Answers (3)

chepner
chepner

Reputation: 531808

The braces aren't strictly necessary; what you need is at exactly one (possibly empty) statement following the for. You can accomplish this by putting just a semi-colon, terminating an empty statement:

// It's a little more readable to put the semicolon on a line by itself
for (i=0; i<atual->numerChaves; i++)
;

or, as you did, by including an empty compound statement consisting of empty braces.

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263497

No, the braces aren't required, but they're a good idea.

The syntax of a for statement is:

for ( expressionopt ; expressionopt ; expressionopt ) statement

(You can also replace the first "expressionopt ;" with a declaration, but that's not important here.)

The important part is the statement at the end. A for statement requires exactly one statement as part of it.

That can be any kind of statement, including a null statement (consisting of a single semicolon) or a block, also known as a compound statement.

A compound statement consists of an opening {, followed by zero or more declarations and statements, followed by a closing }.

So you can write:

for (i = 0; i < 10; i ++)
{
     printf("i = %d\n", i);
}

but the braces are part of the syntax of the compound statement, not of the for statement. The above may also be written as:

for (i = 0; i < 10; i ++)
     printf("i = %d\n", i);

or even:

for (i = 0; i < 10; i ++) printf("i = %d\n", i);

As a matter of style, I personally prefer to always use a compound statement as the controlled statement of a for (or if, or while, etc.), because (a) it's easier to be consistent, and (b) it's more maintainable; if I want to add a second statement before or after the printf, then the braces are already there.

One thing to watch out for: if you accidentally write:

for (i = 0; i < 10; i ++);  /* extra semicolon */
{
     printf("i = %d\n", i);
}

that's still perfectly legal -- but for controls only the empty statement consisting of that stray semicolon; the block following it is separate, and will be executed exactly once rather than 10 times.

Upvotes: 2

Naetmul
Naetmul

Reputation: 15552

"for" loop in C needs one statement after it.

If you need several statements, then you can enclose them with { and }.
(Of course, you can also enclose zero or one statement.)

And ; can represent an empty statement.

So any of the followings are correct.

for (int i=0; i<10; i++);
for (int i=0; i<10; i++) {}
for (int i=0; i<10; i++) function_that_do_nothing();
for (int i=0; i<10; i++) 1;
for (int i=0; i<10; i++) function_that_do_something();
for (int i=0; i<10; i++) just_a_statement;
for (int i=0; i<10; i++) { statements... }

Additionally,

for (initialization; condition; statement)
    one_statement;

cannot be separated, so the followings are equal.

for (int i=0; i<10; i++)
    for (int j=0; j<10; j++)
        a_statement;

for (int i=0; i<10; i++) {
    for (int j=0; j<10; j++)
        a_statement;
}

for (int i=0; i<10; i++) {
    for (int j=0; j<10; j++) {
        a_statement;
    }
}

The same thing goes for if or while.

Upvotes: 12

Related Questions