473,378 Members | 1,334 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,378 software developers and data experts.

if...else - in which order

this is probably an old topic:

if(a==1)
if(b==2)
echo "a is 1 and b is 2";
else
// what happens here?
echo "a is 1 and b is not 2";

The question is, where the else will react too. Usually it should be
the last if.

In general I put {} around just to be sure.

BR
Sonnich

Nov 1 '06 #1
6 1386
Sonnich wrote:
this is probably an old topic:

if(a==1)
if(b==2)
echo "a is 1 and b is 2";
else
// what happens here?
echo "a is 1 and b is not 2";

The question is, where the else will react too. Usually it should be
the last if.

In general I put {} around just to be sure.

BR
Sonnich
Hi,

Use {} to note (to yourself and PHP) what you are doing.
The confusion you have is created by yourself. :-)

Try this:

if(a==1){
// a is 1
if(b==2){
// b is 2
echo "a is 1 and b is 2";
} else {
// b is not 2
echo "a is 1 and b is not 2";
} // end inner if-then-else
} // end outer if

Regards,
Erwin Moller
Nov 1 '06 #2
Sonnich wrote:
this is probably an old topic:

if(a==1)
if(b==2)
echo "a is 1 and b is 2";
else
// what happens here?
echo "a is 1 and b is not 2";

The question is, where the else will react too. Usually it should be
the last if.

In general I put {} around just to be sure.

BR
Sonnich
'else' clauses are always matched with the first unpaired 'if' preceding
them.

Using braces does make things clearer, though.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 1 '06 #3
Sonnich wrote:
The question is, where the else will react too. Usually it should be
the last if.
It always matches the inner-most /if/ for which there are no interleaving
statements. (There is no "usually" about it.)
In general I put {} around just to be sure.
When nesting /if/s and using /else/, then it's certainly a good idea to
use the braces to indicate what is nested within what else. It will spare
your sanity a few months down the line if you have to edit that code again!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Nov 1 '06 #4
..oO(David Gillen)
>Nesting if's just leads to confusion in the long run.
Not in properly structured and indented code. With some more braces the
OP's code is perfectly readable without any comment.

But I consider that personal taste.

Micha
Nov 2 '06 #5
Michael Fesser wrote:
.oO(David Gillen)
>>Nesting if's just leads to confusion in the long run.

Not in properly structured and indented code. With some more braces the
OP's code is perfectly readable without any comment.

But I consider that personal taste.
I agree 100%.
Use {} everywhere to make things clear.
And also:
if (a==1 && b==2){

is a mess compared to:

if ((a==1) && (b==2)){

That are just a few extra () but makes the code completely clear and leaves
only 1 interpretation (for both PHP and humans who read the code).

Regards,
Erwin Moller
>
Micha
Nov 3 '06 #6
David Gillen wrote:
An noise sounding like Sonnich said:
>>this is probably an old topic:

if(a==1)
if(b==2)
echo "a is 1 and b is 2";
else
// what happens here?
echo "a is 1 and b is not 2";

Simplify your code by writing more, but it will help you.
if(a==1 && b==2)
{

}
elseif(a==1 && b!=2)
{

}
else....

etc.

Nesting if's just leads to confusion in the long run. Esp 2 months later when
you're looking back at something. Do it the way suggested above and you'll
help to avoid confusion, and will benefit from readability in your code too.

D.
Purely a matter of personal preference. Nested if statements can be
eminently clear if the code is properly indented.

For instance, I find

if ($a==1) {
if ($b == 2) {
echo "a is 1 and b is 2\n";
}
else
echo "a is 1 and b is not 2\n";
}
}
else {
if ($b == 2) {
echo "a is not 1 and b is 2\n";
}
else
echo "a is not 1 and b is not 2\n";
}
}

to be much easier to understand than:

if(a==1 && b==2) {
echo "a is 1 and b is 2\n";
}
elseif(a==1 && b!=2) {
echo "a is 1 and b is not 2\n";
}
if(a!=1 && b==2) {
echo "a is not 1 and b is 2\n";
}
elseif(a!=1 && b!=2) {
echo "a is not 1 and b is not 2\n";
}

I find the second one needlessly complicated with unneeded tests. Also,
the first version requires 2 tests every time. The second version
requires 2, 4, 6 or 8 tests.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 4 '06 #7

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

Similar topics

1
by: Brittany | last post by:
can someone explain to me what if else staments do and what while statements do.
23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
12
by: Roman Töngi | last post by:
In c++ there does not exist an if-statement as for example in Visual Basic, does it? //VB-analogy: if (cond.) statement; else if (cond.) statement; else statement;
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
4
by: Sonnich | last post by:
Hi I have a costum function for a special search, which sort strings. This is currently the place where I can save a lot of time (~70%) if possible. So, which is faster: for($j =...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
2
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.