Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Can the switch statement handle floating point numbers

1. Can the switch statement handle floating point numbers

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:
Code:
```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?

Regards
Dieter

• The format of the switch statement is the following:
Code:
```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.

• 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

• 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.

• If you wish to do comparisons with switch, try this
Code:
```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.

• 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.)

• 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

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•