Defining a variable inside a case switch

Good morning,

I am working on a code where I implement a case switch
if I define a variable inside the switch, I got an error in the program >>> int sensor = analogRead(A1);
However, if I removed the int part, the program is working probably.
What is the problem, I can not define variable inside the switch ?

switch (var) {
  case 'A':
     W_ctr_8bit(0b10000000);
     int sensor = analogRead(A1); 
     valueLCD(sensor);
     var = '0'; 
    break;
  case 'B':
    W_ctr_8bit(0b00000001);
    W_data_8bit('x');W_data_8bit('y');W_data_8bit('z');delay(2000); 
    var = '0'; 
    break;
  
}

Create a code block for the case code by putting it in { and }

  case 'A':
  {
     W_ctr_8bit(0b10000000);
     int sensor = analogRead(A1); 
     valueLCD(sensor);
     var = '0'; 
    break;
  }

Or skip the local variable creation:

  case 'A':
     W_ctr_8bit(0b10000000);
     valueLCD(analogRead(A1));
     var = '0';
    break;

UKHeliBob:
Create a code block for the case code by putting it in { and }

  case 'A':

{
    W_ctr_8bit(0b10000000);
    int sensor = analogRead(A1);
    valueLCD(sensor);
    var = '0';
    break;
  }

Can you expand more on this approach?
I dont believe I have ever saw this syntax approach before?
I think my approach would have been declare it globally (outside of switch/case) statement.. update it if needed in the switch/case snippet...
but I'd like to understand this approach better.
Thanks

The OP dis not post the exact error, but it should have been

crosses initialization of ’ int sensor ’

A Google search of “crosses initialization error” will give plenty to read about switch cases, scope, and variable declarations.

Global declaration or {} to limit the scope to the case are the two solutions.

Global declaration or {} to limit the scope to the case are the two solutions.

Doesn't need to be global as long as it's declared before entering the switch.