Advertisement

'Case' expression question

Started by July 07, 2000 01:20 AM
2 comments, last by SikCiv 24 years, 5 months ago
Instead of this: switch(sw) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9://Code.. Check(sw); break; } Can u save a few hundred lines and do something like: switch(sw) { case 1 to 9://Code.. Check(sw); break; } ?

  Downloads:  ZeroOne Realm

erm no you cant.

But instead of the switch statement you can do:

if ( sw >= 1 && sw <= 9)
{
Check( sw);
}
else
{
// Not valid
}
Advertisement
Does the Pentium series even support an indexed jump table instruction? Obviously, it can be emulated with a few instructions.

I remember even on the PDP (where C got it''s start) and VAX systems, the compiler would many times replace case statements with if-else clauses because they were more efficent depending on case statement.

Tim
I''m writing a board game that has repeated tiles at different intervals, so I have to use a weird multi-case thing too. I found it easier on the eyes if I just put all the cases on one line:
    switch (num){  case 1: case 4: case 6: case 9: case 13:    do_tile_A (); break;  case 2: case 5: case 8: case 12:    do_tile_B (); break;  default: ASSERT (0); // heh heh}    

This topic is closed to new replies.

Advertisement