Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > if statement

Reply
Thread Tools

if statement

 
 
jraul
Guest
Posts: n/a
 
      05-18-2007
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.

 
Reply With Quote
 
 
 
 
antonov84@ukr.net
Guest
Posts: n/a
 
      05-18-2007
On May 18, 4:40 am, jraul <jrauli...@yahoo.com> wrote:
> 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



 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      05-18-2007
On May 17, 10:40 pm, jraul <jrauli...@yahoo.com> wrote:
> 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;
}

 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      05-18-2007
> > 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.


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

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      05-18-2007
On May 17, 10:40 pm, jraul <jrauli...@yahoo.com> wrote:
> 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;
}



 
Reply With Quote
 
Kar
Guest
Posts: n/a
 
      05-18-2007
On May 18, 7:40 am, jraul <jrauli...@yahoo.com> wrote:
> 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

 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      05-18-2007
jraul wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57