PalashV
PalashV

Reputation: 799

Does compiler sees these uses of else-if as the same thing?

Instead of this -

if(*condition1*){
    *statement/s*
}
else if(*condition2*){
    *some different statement/s*
}

if this is used -

if(*condition1*){
        *statement/s*
}
else
    if(*condition2*){
        *some different statement/s*
    }

Does the compiler sees both of these as same things, because white-space does not matter in C, and the 2nd if gets associated with the else statement.

Similarly, for this, do they mean the same?

for(*initialization*;*condition*;*increment*)
    ;

and this -

for(*initialization*;*condition*;*increment*);

where ; is a null statement.

If yes, then why?

Upvotes: 1

Views: 67

Answers (5)

A.G.
A.G.

Reputation: 1319

All leading and trailing whitespaces are ignored.

Upvotes: 1

sharon
sharon

Reputation: 734

The else if conditions are same. but the for loop is different, because,

for(*initialization*;*condition*;*increment*)
                ;

here before the ; you can give the statements to execute and it will be executed. but if you specify the ; without the statements then if the condition is true nothing will be done.

Upvotes: 1

SMA
SMA

Reputation: 37023

Yes both if and for statements are the same. For C, Space, newline doesn't matter.

Upvotes: 2

llogiq
llogiq

Reputation: 14511

Yes. Whitespace (apart from its presence at all) truly is insignificant in C. Most C implementations collect whitespace while lexing.

Upvotes: 1

Daij-Djan
Daij-Djan

Reputation: 50089

It doesnt matter at all if you add newlines or whitespaces

Upvotes: 2

Related Questions