Dieter Rausch
01-09-2004, 07:00 PM
Hi Everyone,
I am experimenting with a switch statement in order to avoid using nested if... else statements. However, it appears there are certain limitations with regard to the usage of operators. For example the following code works fine:
var x=2;
switch (x)
{
case 2:
var y=2
break;
case 3:
var y=3
break;
default:
alert("Value of y: " +y);
}
But, once I use
[code]
var x=2;
switch (x)
{
case =2:
var y=2
break;
case 3:
var y=3
break;
default:
alert("Value of y: " +y);
}
[c/code]
I get a syntax error message. Similarly when using [case >=2:] or [<=]. Using ["<=2"] avoids the error message, but 'var y' is not being calculated. I have seen examples where string variables such as "Oranges" and "Apples" etc. where used as well as integers. But what happens in the case when x is a floating point number and one needs to determine whether x <=2, or, x > 2 but smaller than say 5. Can this be done with the switch statement or is a nested if... else more appropiate?:confused:
Please clarify.
Regards
Dieter
I am experimenting with a switch statement in order to avoid using nested if... else statements. However, it appears there are certain limitations with regard to the usage of operators. For example the following code works fine:
var x=2;
switch (x)
{
case 2:
var y=2
break;
case 3:
var y=3
break;
default:
alert("Value of y: " +y);
}
But, once I use
[code]
var x=2;
switch (x)
{
case =2:
var y=2
break;
case 3:
var y=3
break;
default:
alert("Value of y: " +y);
}
[c/code]
I get a syntax error message. Similarly when using [case >=2:] or [<=]. Using ["<=2"] avoids the error message, but 'var y' is not being calculated. I have seen examples where string variables such as "Oranges" and "Apples" etc. where used as well as integers. But what happens in the case when x is a floating point number and one needs to determine whether x <=2, or, x > 2 but smaller than say 5. Can this be done with the switch statement or is a nested if... else more appropiate?:confused:
Please clarify.
Regards
Dieter