NO

Author Topic: switch case  (Read 4863 times)

ahsjar

  • Guest
switch case
« on: June 15, 2007, 09:53:29 AM »
hello,
shouldn't this work?

Code: [Select]
switch (x)
{
    case a:
    case b:
    case c:
         ...;
         break;
    case d:
         ...;
         break;
    case e:
         ...;
         break;
}

but i am getting the following error (for lines at "case b:" and "case c:")
error #2001: Syntax error: found ':' - expecting ';'.
error #2061: Illegal statement termination.


ahsjar

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: switch case
« Reply #1 on: June 15, 2007, 10:45:41 AM »
In the switch statement the cases must be constant integers.
In your code a, b, c, etc are constant integers?
The use of multiple cases with the same body is perfectly legal.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

ahsjar

  • Guest
Re: switch case
« Reply #2 on: June 15, 2007, 11:50:05 AM »
thanks for the quick reply..

yes, they are constant integers and i tested it - in the same code this works:

switch (x)
{
    case a:
         ...;
         break;
    case b:
         ...;
         break;
    case c:
         ...;
         break;
}
 
and this doesn't:

switch (x)
{
    case a:
    case b:
    case c:
         ...;
         break;
}

hmm... i wonder what it is?  ???

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: switch case
« Reply #3 on: June 15, 2007, 12:17:33 PM »
This sample:
Code: [Select]
int a,b,c,d, x;

x='a';
a=b=c=d='x';

switch(x)
{
case 'a':
case 'x':
case 'z':
case 'f':
case 'j':
case 'w':
case 'y':
a=b;
break;
case 'b':
b=c;
break;
case 'c':
c=d;
break;
case 'd':
d=a;
break;
}
printf("%d%d%d%d%d", a, b, c, d, x);

switch(x)
{
case 'a':
case 'b':
case 'c':
case 'd':
d=a;
break;
}
Works without problems here.
What really are a, b, c etc?
This snippet gives a similiar error:
Code: [Select]
#define d 'd':

switch(x)
{
case 'a':
case 'b':
case 'c':
case d:
c=a;
break;
}
Could you post a snippet of your code, possibly a simple project with the options you have set.
« Last Edit: June 15, 2007, 12:25:32 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

ahsjar

  • Guest
Re: switch case
« Reply #4 on: June 15, 2007, 05:08:10 PM »
hey Frankie!
sorry i took your time... the problem was that i was staring at the screen so long that i just didn't see that i wrote:

case a:
       b:
       c:
       ...;
       break;

but this looks interesting:

Code: [Select]
#define d 'd':

switch(x)
{
case 'a':
case 'b':
case 'c':
case d:
c=a;
break;
}

well, now i repaired it and it works! it seems i have to learn how to look at the things (not just code). sorry once again and thanks!

ahsjar