switch statement: Is it possible to include something like "Case var > 5" in a case statement?

  • Thread starter Thread starter Juan
  • Start date Start date
No. But you can do:

switch (n)
{
case 1:
case 2:
case 3:
case 4:
DoFoo();
break;
case 5:
DoBar();
break;
}

Note that fallthrough is only allowed if the case block is empty, otherwise
you have to state a "break" for every "case".
 
No, it has to be a constant integral or string expression. What you can do
it the following

int myInt = ...;

switch (myInt)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// Do something here for 0 <= myInt < 5
break;
case 5:
case 6:
// Do something here for myInt = 5 or 6
break;
}

something like that.

Hope this helps.
 
Thanks, got it.

cody said:
No. But you can do:

switch (n)
{
case 1:
case 2:
case 3:
case 4:
DoFoo();
break;
case 5:
DoBar();
break;
}

Note that fallthrough is only allowed if the case block is empty, otherwise
you have to state a "break" for every "case".
 
Thanks, got it.

Jako Menkveld said:
No, it has to be a constant integral or string expression. What you can do
it the following

int myInt = ...;

switch (myInt)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// Do something here for 0 <= myInt < 5
break;
case 5:
case 6:
// Do something here for myInt = 5 or 6
break;
}

something like that.

Hope this helps.
 
Juan,

I thought I'd point out the "why" behind the "no, you can't".

In C, C++, Java, and now C#, the switch / case construct is intended as
a high-speed choice between multiple alternatives. Some languages (such
as COBOL) allow conditions as tests for each of the branches, which is
still a "choice between multiple alternatives," but it's slow. If you
were allowed to do something like this:

switch (myInt)
{
case > 0 && < 5:
DoSomething();
break;
case >= 5:
DoSomethingElse();
break;
default:
Error();
break;
}

Then the compiler would have little choice but to generate code that
tested the conditions one-by-one until it found one that matched. (I'll
leave aside the semantics of what to do if more than one condition were
true.) In other words, the compiler would have to generate the same
code as if you had said:

if (myInt > 0 && myInt < 5)
{
DoSomething();
}
else if (myInt >= 5)
{
DoSomethingElse();
}
else
{
Error();
}

Instead, the switch...case in the C/Java school is intended to be
compiled into a high-speed lookup. The compiler does _not_ generate
code to test one case after another, in sequence, until it finds the
matching one. (That is, unless it's a profoundly stupid compiler.)
Instead, it generates a rapid lookup scheme to find the correct "case"
branch in the minimum amount of time. There are, of course, many
strategies, from binary searches through the case list to hash tables
of pointers to the code to run. Regardless, the idea is that in a
switch statement with 200 cases you don't have to test up to 200 times
in order to find out what to do.

The only optimization of this structure I've seen was in Pascal, which
allowed you to say

case 1..15:

as a shortcut for repeating

case 1:
case 2:
....
case 15:

but it still didn't allow you to put full-blown boolean conditions in
the "case" statements, for the above reasons.
 
Back
Top