#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 15, 2009 17:29:28 GMT -8
You can do all kinds of weird things like that in Javascript. I don't think most programming languages would allow that, though. I saw that trick originally in PHP. However, I'm not sure about other languages. I think I remember something about Java being very strict with Switch statements and only allowing certain types of variables to be used. I guess I should have said scripting languages allow those types of things, but most programming languages don't. I know Java like you said and C++ don't allow it.
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Nov 15, 2009 21:45:52 GMT -8
Other than switch() allowing multiple combinations of events, e.g.
switch ($x) { case 1 : echo 'x'; case 2 : echo 'y'; } // outputs xy when x = 1, y when x = 2 I use it when one would go through a list of if statements just to check the value of a single variable. The advantage of if over switch is: if ($some_variable == true) { /* do whatever */ } else if ($something_else == true) { /* do whatever else */ } With if, the else statements don't have to have anything to do with the original if, which is good when comparing multiple values. e.g. if (isset($song)) { /* display song page */ } else if (isset($artist)) { /* display artist page */ } else { /* display navigation */ } This will display song as long as song is set, even if artist is set. Whereas switch cannot compare both song and artist in order to determine which to display.
However, when the same variable is being checked repetitively: if ($song == 1) { /* display song 1 */ } else if ($song == 2) { /* display song 2 */ } else if ($song == 3) { /* display song 3 */ } else if ($song == 4) { /* display song 4 */ }
And with switch(), switch ($song) { case 1 : display(1); break; case 2 : display(2); break; case 3 : display(3); break; }
However, many times switch() can be replaced by an array or some such thing, which is why switch() is so rare. If each case is similar, one could just do display($song) or echo $lyrics[$song]. switch() really shines when each value has a drastically different effect that cannot be simplified to an array of some sort, and if statements would simply be looooooong and repetitive.
|
|
inherit
12045
0
Nov 19, 2012 14:52:05 GMT -8
Renegade
As unique as mice pudding milkshake
40,557
August 2003
renegade
|
Post by Renegade on Nov 17, 2009 3:56:31 GMT -8
its also useful when more than one part fo the statement is true, if you don't use break, since it will evaluate every condition.
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Nov 17, 2009 11:09:48 GMT -8
Very true.
if ($x) { stuff(1); if ($y) { stuff(2) if ($z) { stuff(3); } } } else stuff('etc');
Could be extremely simplified with switch();
|
|
inherit
125499
0
Nov 8, 2011 4:03:57 GMT -8
moneyman18
:-
952
June 2008
moneyman18
|
Post by moneyman18 on Nov 17, 2009 19:01:34 GMT -8
Thanks Everyone! charles - In your last post how would that be simplified with a switch? How would those variables even be used in one the same way you used them in the if's is what I'm not understanding. Could you possibly show an example?
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Nov 17, 2009 22:06:07 GMT -8
switch allows you to flow from one if statement to another if they're within each other. The switch() alternative would be:
switch (true) { case $x : stuff(1); case $y : stuff(2); case $z : stuff(3); break; default : stuff('etc'); }
Since there is no break after $x or $y, it will continue to run and check $y and $z. switch() runs everything after case until it reaches a break. In the if() example I listed, we want it to keep running until after $z.
Although, switch() may actually run both stuff(1) and stuff(3) if $x and $z are true but $y is not. Not sure about that. So maybe my example wouldn't fit the situation as well if that's the case.
|
|