Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch statement

Status
Not open for further replies.

mcm103

Technical User
Sep 28, 2001
20
0
0
US
I need help with the syntax of a switch statement. I have a variable that contains a calculated value. Based on that value I want something to happen.

between 0 and 1: x happens
between 1 and 2: y happens
etc.

I am having trouble coding the case for a range.
 
[tt]
switch (a){
   case (a>0 && a<1):
      //x happens;
      break;
   case (a>1 && a<2) :
      //y happens;
      break;
}
[/tt]
 
I don't think the switch statement is what you need here. This is one place where you should use if...else if...else if. As well, what happens if the values are exactly 1 or 2 or some other integer?

if (a>0 && a<=1)
{
....
}
else if (a > 1 && a<=2)
{
.............
}
//etc.
 
This is what I tried and for some reason the switch will not execute. I have but the same range logic in IF/ElseIf statements and it works. I am very confused.
 
Maybe you're right. All the examples I ever saw use only string values for switch labels.
There is no anything about some limitations in Netscape JS guide though.
I don't like it so much and usually use &quot;if ... else if&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top