C++ Question on variable declaration within switch statement

I have a switch statement with 6 cases. I wanted to declare some variables within case 5 that would only be used in case 5. The following did NOT work:

case 5:  
      int tempDegF = static_cast<int>(rtcDS3231.getTemperature()*1.8+32);
      int startingY = map(tempDegF, -10, 110, 46, 21); 
break;
case 6:
break;

but this did

case 5: 
      int tempDegF;
      int startingY;
      tempDegF = static_cast<int>(rtcDS3231.getTemperature()*1.8+32);
      startingY = map(tempDegF, -10, 110, 46, 21); 
break;
case 6:
break;

and this did as well

case 5:  
      int tempDegF = static_cast<int>(rtcDS3231.getTemperature()*1.8+32);
      int startingY = map(tempDegF, -10, 110, 46, 21); 
break;
// with case 5 as the final case in the switch statement

why is this?

Good question, that would be a good question towards https://stackoverflow.com/.

The one thing I can say is that in order to avoid these problems, create a new scope by using {} and create and use the variables in there, or declare the variables outside the switch statement, in the function body above. So

would work perfectly too.

This explains it in detail:

This also explains why it works in the last case (case 5).

I see. Thank you guys for that explanation. This is a nuance I was unaware of.

Fish