switch...case

Hello,

The Reference-SwitchCase page is a mistake:

switch (var) {
  case 1:
    //do something when var equals 1
    break;
  case 2:
    //do something when var equals 2
    break;
  default: 
    // if nothing else matches, do the default
    // default is optional
}

That should be:

switch (var) {
  case 1: {
    //do something when var equals 1
    break;
  }
  case 2: {
    //do something when var equals 2
    break;
  }
  default: {
      // if nothing else matches, do the default
      // default is optional
  }
}

No, there's nothing wrong with the first.

If you want braces, the following format is more common, but they're not required (Duff's Device would look crazier than it already does if they were)

switch (var) { case 1: { //do something when var equals 1 } break;

case 2: { //do something when var equals 2 } break; default: // if nothing else matches, do the default // default is optional }

i get:

The first: error: expected primary-expression before '}' token

Yours: error: expected primary-expression before '}' token

Mine: Works :)

Groove is correct that the braces are not necessary for the “case”; however, if you have a “default”, it has to do something, even if that “something” is
empty braces or a “break”.

switch (var) {
 case 1:
 break;

 case 2:
 break;

 default: {}    // {} or "break;"
}

(I think this is a recent restriction; I don’t remember “default” having to do something, but it’ll take a while to find a old enough compiler)

Yes, but the Example:

  switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
  }

If you simply copy it will not work.

(I think this is a recent restriction; I don't remember "default" having to do something, but it'll take a while to find a old enough compiler)

Ah, the joy of version-controlled tools... Our gcc (x86 linux) 2.95 and 3.4.5 don't mind having an empty default:, but 3.4.6 does; it reports "label at end of compound statement" (our 2.95 and 3.4.5 compilers are locally modified, while the 3.4.6 is stock redhat. YMMV.)

If you simply copy it will not work

Mostly because it simply doesn't do anything. Repeat: there is nothing wrong with the example - the compiler is at fault here.

AWOL, it may well be the compiler that is at fault, but to a newbie like myself it would cause an error. An error is when something does not work for some reason.

I do hear what you are saying. and THANK YOU and all the others for all the help ya'll provide.

Ken H.

The more traditional "fix" would be to include a "break;" after the default case as well.

Note that the "default" label need not be the last in the case statement. Without a "break", it could potentially fall through into the next case...

The thing to remember about a switch statement is that once the execution matches the value after "case", it starts executing statements until it executes a break. The braces aren't needed. If you executed:

switch (i) {
case 1: j=1;
case 2: j=2;
case 3; j=3;
default: j=4;
}

then j will always be 4.

a switch like:

switch (i) {
case 1: { a=1; b=2;} break;
case 2: { a=-1; b=-2;} break;
}

is exactly the same as

switch (i) {
case 1:  a=1; b=2; break;
case 2:  a=-1; 
         b=-2; 
         break;
}

or

switch (i) {
case 1: { a=1; b=2; break; }
case 2: { a=-1; b=-2; break; }
}

or even

switch (i) {
case 1: { a=1;} b=2; break; 
case 2: { a=-1;} b=-2; break; 
}

Note that the "default" label need not be the last in the case statement

Some in-house coding standards I've worked on [u]require[/u] it to be at the top of the "switch" - makes it easier to ensure it's there and doing what it's supposed to.