473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

if statement

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.

May 18 '07 #1
6 1433
On May 18, 4:40 am, jraul <jrauli...@yahoo.comwrote:
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

May 18 '07 #2
On May 17, 10:40 pm, jraul <jrauli...@yahoo.comwrote:
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;
}

May 18 '07 #3
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

May 18 '07 #4
On May 17, 10:40 pm, jraul <jrauli...@yahoo.comwrote:
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;
}

May 18 '07 #5
Kar
On May 18, 7:40 am, jraul <jrauli...@yahoo.comwrote:
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

May 18 '07 #6
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
May 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
23
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.