Forum Moderators: open

Message Too Old, No Replies

necessity of the 'else'

         

SarK0Y

10:44 pm on Dec 7, 2008 (gmt 0)

10+ Year Member



Good time to All, Amigos!

probably, it's strange, but i don't see fit to use operator 'else': common structure is:
if(...)
{
//any code
}
else
{
// any code
}
-------------------
why should this form may be better than:
if(...)
{
//any code
}
// any code
?{oo}

phranque

11:42 pm on Dec 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



a demonstration:
if (i am green)
{say "i am green"}
else
{say "i am not green"}

------------------
versus:
if (i am green)
{say "i am green"}

say "i might (not) be green"

------------------
if you try the first test as green the result is:
"i am green"

if you try the second test as green the result is:
"i am green"
"i might (not) be green"

brotherhood of LAN

11:46 pm on Dec 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yep phranque has a good example

Though I do find myself writing something like

if(not doing what you're supposed to be doing)
{ get out of my script }

continue nicely...

SarK0Y

12:47 am on Dec 8, 2008 (gmt 0)

10+ Year Member



Hi, phranque.
i know your e.g. but necessary to mark one thing: code consists of threads 'if' and 'goto': common form without 'goto':
if(...){....}
if(...){....}
............
if(...){....}
else{....}
as we can see, 'else' at the end of the code.
----------------
even for form:
if(...){....}
else{....}
............
if(...){....}
else{....}
we can throw away it without damage of the code's logic.

[edited by: SarK0Y at 1:42 am (utc) on Dec. 8, 2008]

SarK0Y

12:54 am on Dec 8, 2008 (gmt 0)

10+ Year Member



many occurrences allow to except 'else'.

Anyango

7:41 am on Dec 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?
echo "is Anyango a Fool ?";
$answer="No";

if($answer=="No")
{
echo "No ";
}
echo "Anyango is a Fool";

Go run it, it will say "No Anyango is a Fool"

Thats why an else was needed there which i didn't put so that makes me a Fool ;)

?>

eelixduppy

8:55 am on Dec 8, 2008 (gmt 0)



>> code consists of threads 'if' and 'goto'

We generally do not use goto's that often in writing conditionals. An else statement is certainly cleaner, too. Stick with it and you'll be good. :)

The only time I can think that it is necessary to use a "goto" for a conditional, or in this case a "branch", would be in assembly language. And by the looks of your pseudocode you're far from coding that. ;)

phranque

11:40 am on Dec 8, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i'm not sure exactly what question you are asking, SarK0Y.
you can certainly leave the else statement out if it is not necessary.
if you are using a goto to avoid the else, don't.
i can't remember the last time i coded a goto.

what is your opinion of the elsif statement?
=8)

SarK0Y

2:57 pm on Dec 8, 2008 (gmt 0)

10+ Year Member



Amigos, i don't argue that 'else' is useful. i often use 'else' in others langs(for example, AS3) but php code rarely needs this operator due to its specificity.

SarK0Y

3:21 pm on Dec 8, 2008 (gmt 0)

10+ Year Member



eelixduppy, 'goto' is deprecated but i love this operator:)) i have learned assembler but you're right - i'm very far from it & high-level langs are more near to me, (especially AS3), now.

jatar_k

5:58 pm on Dec 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you find a goto in high level languages there's a definite problem

if you're not using else then my guess is a long string of ifs is usual, most often the logic can be clarified using else or even simplified or sped up, if you select the right option for the if instead of the else.

SarK0Y

7:48 pm on Dec 10, 2008 (gmt 0)

10+ Year Member



Hi, jatar_k.
any code may be without 'else':
if(...){
x=true;
}else x=false; ==>
x=false;
if(...)x=true;
but! if we define large array except 'else' is idiot way very much. and i mark once more, php rarely needs 'else' usual code:
if(...){
.............
exit();
}
if(...){
.............
exit();
}
...........
if(...){
.............
exit();
}
'else' is odd here. php doesn't fit for algos with hard calculations and this is general cause rare necessity an 'else'.

rocknbil

3:58 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, I'll bite. Let's say you have a login form, you know the ones.

$login = '<form..... (username, pass, submit . . . </form>';

We want to output an instructional message above the form. To avoid confusing the users, we want the message to be specific to the current state.

if ($error) { $msg = "An error has occurred: $error. Please try again."; }
else { $msg = "Please log in with your email address and password."; }

$total_output = $msg . $form;

So we want the form to output in either case, but the message varies based on the conditions.

Even a ternary/short circuit evaluation is an "else"

$msg = ($error)?$login_msg:$errmsg;

How would you do that without an else? Without an else, you get redundant messages - both the error and the log in instructions. People don't like to read. It may only take that one extra line of text for them to leave your site and tell everyone it's broken.

if ($error) { $msg = "An error has occurred: $error. Please try again."; }
$msg .= "Please log in with your email address and password.";
$total_output = $msg . $form;

Using else is one more tool by which you can obtain specificity in your code, use it if you need it. Eventually, you'll stumble on one.

SarK0Y

7:55 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Hi, rocknbil.
firstly, my words:
>>>>>>but! if we define large array except 'else' is idiot way very much.
your example solves so:
$msg .= "Please log in with your email address and password.";
if ($error) { $msg = "An error has occurred: $error. Please try again."; }
$total_output = $msg . $form;
my words touch same cases:
for($i=0; $i<10000)myarray[$i]=func(...);
if($err)for($i=0; $i<10000)myarray[$i]=func1(...);
//any code
it's without 'else', but is stupid: myarray is assigned twice with probability 50%.

[edited by: SarK0Y at 7:58 pm (utc) on Dec. 11, 2008]