...

View Full Version : quick switch case question



live4soccer7
01-05-2011, 07:01 AM
Here is what i'm trying to do and I'm not sure if it will work with a switch, but i'd be better than writing a million if else statements.


var = c
var = z

switch (c)
{
case c > 1 && < 10:
z = 5;
break;
case c > 10 && < 20:
z = 15;
break;
default:
z = 0;
}

I think you see what I'm trying to do. I have a variable 'c' and it is input by the user and I want to make variable 'z' set according to what 'c' is. I know the above code doesn't work, but I'm not sure why. I've tried a million different things to try and get it to work, but it always just goes to default when it displays 'z'. Any help would be great. Thanks.

Philip M
01-05-2011, 09:20 AM
You might do better to read the manual.

The case labels in a switch statement are expressions whose values are compared to the value of the switch expression, not Boolean expressions.

var = c
var = z

You can use also a switch as if it were a regular if/else but it is not normal to do so as it becomes in effect a simple if/else statement.


var c = 13;
var z;

switch (true) {
case (c>1 && c <10):
z = 5;
break;
case (c > 10 && c < 20):
z = 15;
break;
default:
z = 0;
}
alert (z);


It is your responsibility to die() if necessary….. - PHP Manual



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum