Can't have data declarations in switch statements?

   #include <Wire.h>

#define SENDDATA 4
#define REPORT 3

#define sensorPin1 1
#define sensorPin2 2
#define sensorPin3 3
int command=0;
void setup()
{
    switch (command) {
        case SENDDATA:
            int sensorValue = analogRead(sensorPin1);
            Wire.send(sensorValue);
            sensorValue = analogRead(sensorPin2);
            Wire.send(sensorValue);
            sensorValue = analogRead(sensorPin3);
            Wire.send(sensorValue);
            break;
        case REPORT:
            Wire.send(4);
            break;
    }
}
void loop() {
}

Fails with arcane

In function 'void setup()':
error: jump to case label

And points to the case REPORT: statement.

Move the int sensorValue outside of the switch statement and it works.

Why? This is valid C++ code. Data declaration statements are not confined to the top of the function with this compiler normally.

Jim

I've worked in other systems that didn't allow local variables in case statements, except inside a block.

case 3:
   int w=4;
   break;

was illegal, but:

case 3:
   if(i==6)
   {
      int w=4;
   }
   break;

was legal.

Depends on how the compiler write read the specification, I guess.

In your case, you don't need the local variable sensorValue. You could just do this:

Wire.send(analogRead(sensorPin1));

you can have data declarations in a code block - adding brackets should fix the problem:

        case SENDDATA:
         {
            int sensorValue = analogRead(sensorPin1);
            Wire.send(sensorValue);
            sensorValue = analogRead(sensorPin2);
            Wire.send(sensorValue);
            sensorValue = analogRead(sensorPin3);
            Wire.send(sensorValue);
         }
            break;

edit: was composing this when the Paul posted the above reply

The problem is that int sensorValue = analogRead(sensorPin1); isn't just a declaration, it has an initializer too. The rule is you aren't allowed to jump over initializers (the error message used to be a lot more helpful and said that explicitly I seem to remember).

The reason for this is quite hard to explain, it is easier to understand if instead of an int the variable is an object from a class with several different constructors. If the initializer is skipped over, but then the object is used later on, the object won't have been properly constructed, and the compiler won't have known which constructor to call.

You can get round it by removing the initializer and using ordinary assignment:

 case SENDDATA:
         {
            int sensorValue;
                sensorValue = analogRead(sensorPin1);
            Wire.send(sensorValue);
              .....

Thanks, folks. I think I understand. G++ handles it just fine, but I guess the version used for this processor is a bit more restrictive. It was an easy fix to move the declaration out. To whoever simplified my code by suggesting I could merge the read into the write, this was a synthetic sample, not my real code. I needed to save that value for later use in the program.

But I have a lot of options, as was noted.

Thanks,
Jim.

Can anyone tell me why this Switch Case give an error on Case 1:

switch (mode)
{
case 0:
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
break;
case 1: //can't resolve 'jump to case label' error
{
int ledLevel = random(0,ledCount); // use Random gen to simulate audio peaks
break;
}
}

Read the posts above.

Your error is in the case 0 - where you try to declare and initialise sensorReading without using the block markers {..}

switch (mode)
{
  case 0:
     // read the potentiometer:
     int sensorReading = analogRead(analogPin);
     // map the result to a range from 0 to the number of LEDs:
     [glow]ledLevel[/glow] = map(sensorReading, 0, 1023, 0, ledCount);
     break;
 case 1:  //can't resolve 'jump to case label' error
 {
     [glow]int ledLevel[/glow] = random(0,ledCount);  // use Random gen to simulate audio peaks
     break;
 }
}

Local and global variables of the same name is rarely a good idea.

Exactly what error message are you seeing?

Does enclosing the case code in {} as suggested above make any difference?

@ Eight and PaulS. Yes enclosing Case 0 in curly brackets did resolve the problem. Thank you very much for your feedback. :slight_smile: