Simple switch statement question. 
Author Message
 Simple switch statement question.

Sadly no.  And while we're at it, you also cannot use switch statements for
non-constants nor can you use it for strings.

Pretty much it's only useful for:

switch (SomeNumber)
{
case 1:
break;
case 2:
break;
.
.
.
default:

Quote:
}

Note that because case statements "fall through" you can get a similar
effect:

switch (SomeNumber)
{
case 1:
case 2:
case 3:
// do something on 1, 2, or 3
break;
case 4:
// do something else on 4
break;
.
.
.
default:

Quote:
}

--
Reginald Blue                   | Opinions expressed here do not
Natural Language Understanding  | necessarily represent those of
Unisys Corporation              | my employer.
--------------------------------+-------------------------------

NL technology,speech application| My email address is wrong, you
development training, see:      | need to remove the obvious.
http://www.*-*-*.com/ ;   +-------------------------------


Quote:
> I would like to know if there is a way to use the switch statement to test
> for a range of values, for example:

> switch(SomeNumber)
>  {
>       case SomeNumber>10 && SomeNumber<20;
>           cout << "You typed a number between 10 and 20" << endl;

>     case SomeNumber>30 && SomeNumber<40;
>           cout << "You typed a number between 30 and 40" << endl;

>     default:
>          cout << endl << "You entered something." << endl;
>  }



Tue, 14 Oct 2003 03:20:48 GMT  
 Simple switch statement question.
I would like to know if there is a way to use the switch statement to test
for a range of values, for example:

switch(SomeNumber)
 {
      case SomeNumber>10 && SomeNumber<20;
          cout << "You typed a number between 10 and 20" << endl;

    case SomeNumber>30 && SomeNumber<40;
          cout << "You typed a number between 30 and 40" << endl;

    default:
         cout << endl << "You entered something." << endl;
 }

Thank you.



Tue, 14 Oct 2003 03:21:59 GMT  
 Simple switch statement question.

Quote:
> I would like to know if there is a way to use the switch statement to test
> for a range of values, for example:

> switch(SomeNumber)
>  {
>       case SomeNumber>10 && SomeNumber<20;
>           cout << "You typed a number between 10 and 20" << endl;

>     case SomeNumber>30 && SomeNumber<40;
>           cout << "You typed a number between 30 and 40" << endl;

>     default:
>          cout << endl << "You entered something." << endl;
>  }

No, that is what the if/else construct is for - what have you got against
it? In this case it actually results in less code!

NeilB



Tue, 14 Oct 2003 02:48:12 GMT  
 Simple switch statement question.


Quote:
> I would like to know if there is a way to use the switch statement to test
> for a range of values, for example:

> switch(SomeNumber)
>  {
>       case SomeNumber>10 && SomeNumber<20;
>           cout << "You typed a number between 10 and 20" << endl;

>     case SomeNumber>30 && SomeNumber<40;
>           cout << "You typed a number between 30 and 40" << endl;

>     default:
>          cout << endl << "You entered something." << endl;
>  }

> Thank you.

You could, if so inclined, define a function that would map the ranges you
are interested in to single numbers, e.g., Function(n) would return 1 if n
was between 10 and 20, return 2 if n was between 30 and 40 and so on. You
could then enter

switch(Function(SomeNumber))
{
        case 1:
            cout << "You typed a number between 10 and 20" << endl;
        case 2:
            cout << "You typed a number between 30 and 40" << endl;
 etc. etc.

Quote:
}

With more effort, you could define a more general function that could be
used in a wide class of cases. Presumably Visual Basic and the like do
something like this behind the scenes. Whether this would offer any
advantage over if-else tests is debatable (it probably would in some cases
but in others it would just slow things down).

If you want fixed width ranges, then just dividing will do the trick, e.g.,
if you enter

switch(SomeNumber/10)
{
// stuff

Quote:
}

then case 0 will apply to SomeNumber from 0 to 9, case 1 will apply to
SomeNumber from 10 to 19 and so on. More generally, if you enter

switch((SomeNumber - a)/b)
{
// stuff

Quote:
}

then each case (starting at case 0) will cover b different values of
SomeNumber, starting at SomeNumber = a. Thus case 0 covers SomeNumber values
from a to a+(b-1), case 1 covers the next b values of SomeNumber and so on.

--

Quixote
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)



Tue, 14 Oct 2003 20:17:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. switch statement question

2. Setting a method variable from within a switch statement - Newbie question

3. Question about switch statement.

4. SWITCH Statement Question

5. A couple of simple questions about switch/break/continue

6. simple switch question.

7. Switch Statement

8. Switch Statement Headaches

9. Labels in a switch statement

10. switch statement efficiency

11. user defined switch statements

12. Help needed: Code generation for CASE/SWITCH statements

 

 
Powered by phpBB® Forum Software