switch statement 
Author Message
 switch statement

My switch statement works fine;
(cleaner than using "if{}a == b }else if{{if}{c != d || asdl; !=
kad{else{ if else {e == f && g <= g}else{aaa == bbb;}if{}}else}etc...".)

*However, I haven't seen anything in the docs about using multiple labels;
can I assume it isn't supported?

    -example:
------
if(myVar == "something" || myVar == "something else" || etc...)
-------
    -the || 'or' operator allows comparing a variable to several different
values...

    -but:
------
switch(myVar)
{
case 'something', 'something else' : doSomething();

    (or)

case 'something' || 'something else' : doSomething();

Quote:
}

-------

Looks rediculous and works even worse.

Will I simply use additional 'case' instances?

thanks,

Ken



Tue, 11 Mar 2003 13:42:11 GMT  
 switch statement

function echo()
{
    return typeof(alert) == "object" ?
       alert(arguments[0]) : WScript.echo(arguments[0]);

Quote:
}

var a = 9;
switch (a)
{
    case 1: ; case 2: ; case 3:
        echo("1 2 3");
        break;
    case 4: ; case 5: ; case 6:
        echo("4 5 6");
        break;
    case 7: ; case 8: ; case 9:
        echo("7 8 9");
        break;
    default :
        echo(0);

Quote:
}



:
:
:
: My switch statement works fine;
: (cleaner than using "if{}a == b }else if{{if}{c != d || asdl; !=
: kad{else{ if else {e == f && g <= g}else{aaa ==
bbb;}if{}}else}etc...".)
:
:
: *However, I haven't seen anything in the docs about using multiple
labels;
: can I assume it isn't supported?
:
:     -example:
: ------
: if(myVar == "something" || myVar == "something else" || etc...)
: -------
:     -the || 'or' operator allows comparing a variable to several
different
: values...
:
:     -but:
: ------
: switch(myVar)
: {
: case 'something', 'something else' : doSomething();
:
:     (or)
:
: case 'something' || 'something else' : doSomething();
: }
: -------
:
: Looks rediculous and works even worse.
:
: Will I simply use additional 'case' instances?
:
:
: thanks,
:
: Ken
:
:
:
:
:
:
:
:
:
:
:


Tue, 11 Mar 2003 03:00:00 GMT  
 switch statement
Try this:

switch(whatever)
{
    case 1:
    case 2:
    case 3:
        //code
        break;
    default:
        //code
        break;

Quote:
}

Remember, without break, everything will pass down.


Quote:

> My switch statement works fine;
> (cleaner than using "if{}a == b }else if{{if}{c != d || asdl; !=
> kad{else{ if else {e == f && g <= g}else{aaa == bbb;}if{}}else}etc...".)

> *However, I haven't seen anything in the docs about using multiple labels;
> can I assume it isn't supported?

>     -example:
> ------
> if(myVar == "something" || myVar == "something else" || etc...)
> -------
>     -the || 'or' operator allows comparing a variable to several different
> values...

>     -but:
> ------
> switch(myVar)
> {
> case 'something', 'something else' : doSomething();

>     (or)

> case 'something' || 'something else' : doSomething();
> }
> -------

> Looks rediculous and works even worse.

> Will I simply use additional 'case' instances?

> thanks,

> Ken



Tue, 11 Mar 2003 03:00:00 GMT  
 switch statement

Thanks for the reply, Walter.

I'll need a little time to look over your example and try a some things...
Looks like I may learn a little more than I bargained for.  That's a good
thing!
I'll reply how I get on...

regards,
Ken



Tue, 11 Mar 2003 03:00:00 GMT  
 switch statement

Thanks Mathew and Walter.

Both suggestions were helpful, and I'm all set. Works like multiple e-mail
addresses/recipients with the semicolon...

New question; easy one:

If I have:

    case  'whatever' : return 'something';

does the return statement also accomplish a break by itself, or would it
have to be 'return false'?

thanks once again,

Ken



Tue, 11 Mar 2003 03:00:00 GMT  
 switch statement

Quote:

> If I have:
>     case  'whatever' : return 'something';
> does the return statement also accomplish
>a break by itself, or would it
> have to be 'return false'?

  I would AFAIK say, that it will exit your
  subprocedure and no "break" is needed in this case.

  I read you sort of beeing more happy that
  you can put multiple values into case statement,
  but I would rather not be so. Why ?
  But what is *WEAK* by C(++) and JavaScript
  related to TPascal is that it has no "range type"
  like TPascal has like:

  CASE mynumber OF
  -11,-12 : Begin (* Numbers -11 and -12 *)
              writeln('');
            End;
    0..10 : Begin (* The numbers range between 0 and 10 -> range type*)
              writeln('');
            End;
       11 : Begin (* Number 11 *)
              writeln('');
            End;
  ELSE Begin (*default part.*)
         writeln('None of above numbers');
       End;
  END;

    With JavaScript it is IMPOSSIBLE such construct
    to do .... And thatswhy the code:

  switch(mynumber){
   case -11:
   case -12: /* Numbers -11 and -12 */
            writeln('');
            break;

/* !!!!!!!!!*/
  case (>0)&&(<10): /* The numbers range between 0 and 10 */
            writeln('');
            break;
/* !!!!!!!!!*/

   case  11: /* Number 11 */
            writeln('');
            break;

   default: (*default part.*)
         writeln('None of above numbers');
  }

 doesnt make any sense. Btw the "switch" was introduced
 in JS1.2, so I'd prefer the "if else" construct
 if it doesnt handle small amount of discrete values.
 Also you get your code working then by MSIE3,
 and all pre Netscape4s aswell as Operas, hich dont
 support "switch". And the switch is absolutely
 impotent if it comes to number ranges.
 Make your own conclusions from my posting.

 That is: many of javascripters would be happier
  if range type would be implemented.

MM anno MM  (Marek M?nd aus Estland)
http://my.tele2.ee/cadorsoft/cdr_js_pkb/links.htm



Thu, 13 Mar 2003 03:00:00 GMT  
 
 [ 6 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