jimfog Posted March 19, 2012 Share Posted March 19, 2012 I am using ifelse conditional i an a code segment of my application and i am trying to see if maybe the switch statement is more applicable. The ifelse statement goes like this. if(x>0){...code}elseif(x==0){...code} So can the above converted to this-i have already tried by with no result: switch($x){case ==0: ...codecase >0:...code} Is the above switch code valid? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 19, 2012 Share Posted March 19, 2012 No, that's not valid. A switch statement compares multiple expressions to a single value. "==0" and ">0" are not valid expressions, they don't have a value on the left side. You would need to format it like this: switch (true){ case $x == 0: break; case $x > 0: break;} Link to comment Share on other sites More sharing options...
jimfog Posted March 19, 2012 Author Share Posted March 19, 2012 Ok i saw your code and i noticed that you wrote swich(true). Is the above correct? Just want to be sure, exclude the possibility that this might be a typo error. Link to comment Share on other sites More sharing options...
Don E Posted March 19, 2012 Share Posted March 19, 2012 I believe he is saying that if the case expressions are "true", that particular case will be executed.So if $x == 0 is true, what's in that case will be executed.switch (true){ case $x == 0: // if this ts true // code here is ran break; case $x > 0: // if this ts true // code here is ran break;} Link to comment Share on other sites More sharing options...
justsomeguy Posted March 19, 2012 Share Posted March 19, 2012 Yes. Like I said, a switch statement compares a series of expressions against a single value: switch ([value]){ case [expression]: break; case [expression]: break; ...} Each expression get evaluated and compared with the value. Link to comment Share on other sites More sharing options...
jimfog Posted March 20, 2012 Author Share Posted March 20, 2012 And another thing, are switch statements faster that ifelse statements. I am talking about the case where you have multiple ifelse statements and you replace them with switch. Link to comment Share on other sites More sharing options...
astralaaron Posted March 20, 2012 Share Posted March 20, 2012 Aside from being a lot easier to write and read when you are comparing a lot of == conditions, I found this guys post: http://sun3.org/archives/88 who did a test comparing the speed between the two, switch statements turned out to be about 25% faster he says Link to comment Share on other sites More sharing options...
Genert Posted March 21, 2012 Share Posted March 21, 2012 I would do this in that way: while( true ) { switch( $x ) { // One number stuff comes here case 0: [do something]; break; default:... } if( $x > 92 ) { ... // Other stuff comes here... Link to comment Share on other sites More sharing options...
jimfog Posted March 21, 2012 Author Share Posted March 21, 2012 I would do this in that way:while( true ) { switch( $x ) { // One number stuff comes here case 0: [do something]; break; default:... } if( $x > 92 ) { ... // Other stuff comes here... Ι do not understand what is the reason for using a while loop here. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.