switch statement 
Author Message
 switch statement

The documentation seeems to indicate that the label in a switch statement is
always an equality with the expression. IOW the label value must _equal_ the
expression.

Is this true or is it possible to use other comparison operators (e.g.,
greater than) inside the switch statement?



Thu, 30 Dec 2004 00:53:21 GMT  
 switch statement
David said:

Quote:

>The documentation seeems to indicate that the label in a switch statement is
>always an equality with the expression. IOW the label value must _equal_ the
>expression.

>Is this true or is it possible to use other comparison operators (e.g.,
>greater than) inside the switch statement?

They must be equal.  This is to allow the statement to be compiled
into something much more efficient than a series of if..else
statements.


Thu, 30 Dec 2004 02:15:45 GMT  
 switch statement

Quote:
> The documentation seeems to indicate that the label in a switch statement
is
> always an equality with the expression. IOW the label value must _equal_
the
> expression.

> Is this true or is it possible to use other comparison operators (e.g.,
> greater than) inside the switch statement?

Shamelessly borrowed from Michael Harris's VBScript solution to the same
problem:

  for (i=0; i<10; i++) {
    x = Math.floor(Math.random()*150)+1;
    switch (true) {
      case (x<50):  WScript.echo(x+" is less than 50."); break;
      case (x<100): WScript.echo(x+" is between 50 and 100."); break;
      default:      WScript.echo(x+" is more than 100.");
    }
  }

Warning: Tested in JScript 5.6. This might not work in other versions of
J/Java/ECMAScript!

--
Be humble, if thou would'st attain to wisdom. Be humbler still, when wisdom
thou hast mastered. -Helena Petrovna Hahn Blavatsky

=-=-=
Steve
-=-=-



Thu, 30 Dec 2004 03:19:13 GMT  
 switch statement
Steve:

It works fine in IE6 but fails gracefully in NN4.77 (no error).

Pretty neat but who would have thunk it?

Dave


Quote:


> > The documentation seeems to indicate that the label in a switch
statement
> is
> > always an equality with the expression. IOW the label value must _equal_
> the
> > expression.

> > Is this true or is it possible to use other comparison operators (e.g.,
> > greater than) inside the switch statement?

> Shamelessly borrowed from Michael Harris's VBScript solution to the same
> problem:

>   for (i=0; i<10; i++) {
>     x = Math.floor(Math.random()*150)+1;
>     switch (true) {
>       case (x<50):  WScript.echo(x+" is less than 50."); break;
>       case (x<100): WScript.echo(x+" is between 50 and 100."); break;
>       default:      WScript.echo(x+" is more than 100.");
>     }
>   }

> Warning: Tested in JScript 5.6. This might not work in other versions of
> J/Java/ECMAScript!

> --
> Be humble, if thou would'st attain to wisdom. Be humbler still, when
wisdom
> thou hast mastered. -Helena Petrovna Hahn Blavatsky

> =-=-=
> Steve
> -=-=-



Thu, 30 Dec 2004 06:03:13 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Switch statement

2. Switch statement won't work! (JS/ASP)

3. switch statement

4. Switch statement.

5. Switch Statement

6. can any one tell me why this switch statement will only work once

7. Switch statement and the toLowerCase()

8. Switch Statement

9. switch statement problem

10. Help!!! JScript Switch Statement Does not work on Active Server Page

11. VBS equivilent to JScript 'switch' statement

12. Switch case statement

 

 
Powered by phpBB® Forum Software