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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch statements 1

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
GB
Hia,

Ive got a large switch statement and was wondering if the following code is possible (ive tried compiling various versions but it complains at me.

Code:
switch ( value )
{
  case 1:  doStuff; break;
  case 2:  doStuff; break; 
  case 3:  doStuff; break;
  case 4:  doStuff; break;
  case 5:  doStuff; break;
  case 6:  doStuff; break;
  case 7:  doStuff; break;
  case 8:  doStuff; break;
  case 9:  doStuff; break;
  case 10:  doStuff; break;
[b][red]
  case 11 < 20: doStuff; break;
[/red][/b]
}
rather than to type all the conditions, i.e.
Code:
switch ( value )
{
  case 1:  doStuff; break;
  case 2:  doStuff; break; 
  case 3:  doStuff; break;
  case 4:  doStuff; break;
  case 5:  doStuff; break;
  case 6:  doStuff; break;
  case 7:  doStuff; break;
  case 8:  doStuff; break;
  case 9:  doStuff; break;
  case 10:  doStuff; break;
[b][red]
  case 11:
  case 12:
  case 13:
  case 14:
  case 15:
  case 16:
  case 17:
  case 18:
  case 19:
  case 20: doStuff; break;
[/red][/b]
}

}
 
switch ( value )
{
case 1: doStuff; break;
case 2: doStuff; break;
case 3: doStuff; break;
case 4: doStuff; break;
case 5: doStuff; break;
case 6: doStuff; break;
case 7: doStuff; break;
case 8: doStuff; break;
case 9: doStuff; break;
case 10: doStuff; break;

default:
if( value > 11 && value < 20){ doStuff; }
break;

}

you can put in case statements only constants.

Ion Filipski
1c.bmp
 
i thought that would probably work but since I have multiple ranges that i want to do fixed routines on I wondering if there was another way to do it?

Thanks for the reply.
 
there are no other ways. You may put in case statements only constants. No expressions, no variables, no functions.

Ion Filipski
1c.bmp
 
Some addition:
There was a wonderful SELECT statement in the old good PL/I - we can do that with PLI::SELECT...
It seems more expressive C/C++ way of coding: use if/switch mixture:
Code:
if (value >= 11 && value <= 20)
{
   // do it
}
else switch (value)
{
case 1: do that; break;
...
default:
...
}
But it is a matter of taste...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top