PDA

View Full Version : Can the switch statement handle floating point numbers

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:
}

But, once I use
[code]
var x=2;
switch (x)
{
case =2:
var y=2
break;
case 3:
var y=3
break;
default:
}
[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:

Regards
Dieter

liorean
01-09-2004, 07:23 PM
The format of the switch statement is the following:
switch( switch expression ) {
case case expression: statement list
...
default: statement list
}and the action performed in the switch statement is that the switch expression is compared to the case expression, and if they are equal to eachother, execution of the associated statement list commences. So, the only test you can perform in a JavaScript switch statement is equality. It does handle all possible types of objects, but the comparison is for equality.

=2 as one of your case expressions was, is not a valid expression at all. Neither is <=2. "<=2" on the other hand, being a string literal, is a valid expression, but what you are doing then is that you make a comparison for equality between the switch expression and that string.

You can't specify an operator in the switch statements. They work by equality comparison, and that's it.

Dieter Rausch
01-09-2004, 08:13 PM
Thanks for the quick response. I guess this clarifies it, but I am disappointed by the facts. I have coded a lot in VB and there the Select Case statement is quite powerful and capable of various comparisons. On the other hand, the Select Case statement used in VBScript also seems to be a watered-down version. But I suppose it has it's uses, depending on the application/requirements.

Regards
Dieter

liorean
01-09-2004, 08:18 PM
This is the C/C++ version of the switch statement brought into JavaScript. It's strength is it's speed compared to if..else if..else statements, a speed boost it gets from being simpler.

beetle
01-09-2004, 10:30 PM
If you wish to do comparisons with switch, try this

switch( true )
{
case ( a > b ) :
// code
break;
case ( b > a ) :
// code
break;
default :
// code
}

Note: "switch( true )" doesn't work in NS4.x, in case anyone cares.

liorean
01-09-2004, 11:04 PM
Yeah, but that defeats the purpose of a switch statement anyways, as multiple if statements or an if..else if..else statement does the same and are a little better optimised for it. (Internally speaking, they don't have to enter an expression context since that is already hard coded into the functionality of the statement, so they save a scope resolution.)

Dieter Rausch
01-10-2004, 03:15 PM
Thanks guys,

I have tried both ways and found both to provide the same answers. At the same time I learned something useful about the switch statement, which I am sure will come in handy some time in the future. Thanks beetle.

Thanks liorean for pointing out the optimisation issue. In this particular case I will use the if....else if...else statement.

Regards
Dieter