Instantiating a variable within a switch statement somehow breaks everything

I think I might be going crazy. I have a switch statement which takes in a character. If I declare a variable AND set its value within the switch statement somewhere, any cases after that will never get called (including the default case). Am I missing something here? Note that the type (or name) of "var" does not seem to matter. Is anyone else experiencing the same thing I am?

void setup(){
  Serial.begin(115200);
}

void loop(){
  serialCheck();
}

void serialCheck(){
  if(!Serial.available()){ return; }
  char input = Serial.read();
  switch(input){
    case 'w':
      Serial.println("what");
      break;
    case 't':
      Serial.println("the");
      break;
    case 'h':
      int var = 123; // this line somehow breaks everything
      // int var; // but this is fine
      Serial.println("heck");
      break;
    case 'i':
      Serial.println("is");
      break;
    case 'g':
      Serial.println("going");
      break;
    case 'o':
      Serial.println("on?");
      break;
    default:
      Serial.println("Invalid");
  }
}

Not really... but you have learn't that you should be very careful about creating variables within a switch statement.

Either define it prior to the switch statement, or enclose the relevant case block within { }.

The issue is to do with variable scope. The compiler thinks anything in the lines after your declaration should know about the new variable and it's initial value, but it doesn't in subsequent case blocks because it has not actually been declared.

You will get compiler warnings about this if you have them turned on (recommended).

Well I'll be danged. Good to know. Thanks!

If it makes you feel any better, the firts time this happened to me I wasted huge time trying to figure it out… and got some advices here that 'splained everything.

Thought I was chasing a ghost. Probably lost a few hairs, certainly was tearing at them.

a7

first time this happened to me I googled the error/warning and within seconds got the answer. And the reason is that switch is like goto label, the variable is defined in main scope may or may not get assigned which is nono for the language like C++