value not usable in constant expression in switch case

Hi I'm getting the error the value of 'StepcounterB' is not usable in a constant expression in my code:

int StepcounterA  = 0;
int StepcounterB = 0;
#define Distance  440
void setup() {                
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(A2,OUTPUT);
  digitalWrite(7, LOW); //enable
    digitalWrite(3, LOW); // step
  digitalWrite(2, LOW); //dir
  digitalWrite(9, LOW); // MS 1
  digitalWrite(10, LOW); // MS 2
  digitalWrite(A2, LOW); // analogue pin  
  
  Serial.begin(9600);
}

 void loop() {
  digitalWrite(3, HIGH);
  delay(10);          
    digitalWrite(3, LOW);
    delay(10);  
  StepcounterA = StepcounterA + 1;
  {
  int StepcounterB = analogRead(2);
  StepcounterB = map(StepcounterB, 0, 50, 0,440);
}

  switch(StepcounterA) {
    case StepcounterB:
    delay(1000);
    case Distance:
      if (digitalRead(2) == LOW)
    {
      digitalWrite(2, HIGH);
    }
    else
    {
      digitalWrite(2, LOW);
    }
    delay(500);
    StepcounterA = 0;
  }
  }

case value must be a constant. use if

Check how many variables named Stepcounter you have and what their scope is.

Did you mean to leave the breaks out of the switch/case blocks ?

Also check your scope of StepCounterB

The switch-case construction pre-compiles the tests to allow for efficient range tests and table-lookups
to be generated by an optimizing compiler, so the case labels are constrained to be compile-time constants.

For instance any compile worth its salt does the right thing with code like:

switch (ch)
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  val = 10 * val + ch-'0' ;
  break ;
case '+':
  ......
}

ie it compiles it like:

if (ch >= '0' && ch <= '9')
{
  val = 10 * val + ch-'0' ;
....

(Assuming those characters are contiguous in the character set the compiler uses, typically the case)

Thanks for the replies,

I've now added the breaks in the switch case, however, I am unsure what you mean by scope and how I can change my code to fix the problem

{
  int StepcounterB = analogRead(2);
  StepcounterB = map(StepcounterB, 0, 50, 0,440);
}

in this block, the local variable StepcounterB shadows the global variable StepcounterB . the scope of the local variable int StepcounterB is only inside those {}

A thread to explain scope.