if statement

J

jraul

I have some code like:

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

However, in the debugger, when the first "if" is false, it completely
skips the else. I thought this would be equivalent to:

if( flagIsTrue )
{
if( foo() )
return false;
}
else
{
if( bar() )
return false;
}

but it is not because this latter style works correctly.
 
A

antonov84

I have some code like:

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

Your else hooks to the inmost if, so you better have aligned your code
like this

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

Look, this has lots of logic, for if you tried something like this

if(...)
if(...)
else

and your 'else' would hook to the outmost 'if', you wouldnt be able to
add any more 'elses' as they would be separated from corresponding
inner 'if's :

if(a)
if(b)
else // since this one is outmost (second branch of a)
else // where should this go?

On the contrary, when 'else' hooks to the inmost 'if', you can do
constructs like:

if(a)
if(b)
if(c)
else // alt. branch of c
else // alt. branch of b
else // alt. branch of a
 
S

Salt_Peter

I have some code like:

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

However, in the debugger, when the first "if" is false, it completely
skips the else. I thought this would be equivalent to:

if( flagIsTrue )
{
if( foo() )
return false;}

else
{
if( bar() )
return false;

}

but it is not because this latter style works correctly.

I think you are misinterpreting the results of what you wrote
originally.
Indenting has no effect.
else's scope is simply attached to whatever if-scope was previous to
it.
The compiler is prohibited from taking indentation into consideration
and its not allowed to "guess" where scopes start and end.

if( flagIsTrue )
{
if( foo() )
{
return false;
} else {
if( bar() )
{
return false;
}
}
}

which really amounts to bad logic since you could simply (in psuedo-
code)

if ( Flag AND foo() AND bar() )
{
return false;
}
 
M

Michael

I have some code like:
This is one of the reasons I encourage people to always use the braces
on ifs, even if they're not strictly necessary. (Another reason is
that if someone subsequently modifies the code, it's more likely that
the modification will be correct.) In other words, use your coding
style discipline to help you code more correctly.

Michael
 
S

Salt_Peter

I have some code like:

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

However, in the debugger, when the first "if" is false, it completely
skips the else. I thought this would be equivalent to:

if( flagIsTrue )
{
if( foo() )
return false;}

else
{
if( bar() )
return false;

}

but it is not because this latter style works correctly.


I think you are misinterpreting the results of what you wrote
originally.
Indenting has no effect.
else's scope is simply attached to whatever if-scope was previous to
it.
The compiler is prohibited from taking indentation into consideration
and its not allowed to "guess" where scopes start and end.

if( flagIsTrue )
{
if( foo() )
{
return false;
} else {
if( bar() )
{
return false;
}
}

}

which really amounts to bad logic since you could simply (in psuedo-
code) do as follows

if ( Flag AND ( foo() OR bar() ) )
{
return false;
}
 
K

Kar

I have some code like:

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

However, in the debugger, when the first "if" is false, it completely
skips the else. I thought this would be equivalent to:

if( flagIsTrue )
{
if( foo() )
return false;}

else
{
if( bar() )
return false;

}

but it is not because this latter style works correctly.

Please note , never assume what the skull says while seeing the
program.Its most of the time end with setback. Try with real dry run.
Most of the compiler map each else with nearer if before. Then you
intention is wrong !

Thanks
Karmegam
 
Z

Zeppe

jraul said:
I have some code like:

if( flagIsTrue )
if( foo() )
return false;
else
if( bar() )
return false;

However, in the debugger, when the first "if" is false, it completely
skips the else. I thought this would be equivalent to:

if( flagIsTrue )
{
if( foo() )
return false;
}
else
{
if( bar() )
return false;
}

but it is not because this latter style works correctly.

4 bytes are worth a bug less, aren't they?

Regards,

Zeppe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,212
Latest member
SteveMagga

Latest Threads

Top