Code not exiting While loop

I am building a project where a while loop continues until a button is pressed. The button sets x as 1, which terminates the loop. However, the loop does not end. Any suggestions?

int a = 0; 
int b = 0; 
int x = 0; 
int PotSenValue = 0;
int SoilSenValue = 0;
int ButtonState = 0;  

const int Pump = 2; 
const int LedPin= 1; 
const int Button = 0; 
const int SoilSen = A2; 
const int PotSen = A3; 



void setup() {
  pinMode(Pump, OUTPUT);
  pinMode(LedPin, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
 
  
  while (x < 1) {

   int PotSenValue = analogRead(PotSen);
  
   if (PotSenValue < 505) {
    int a = 0.5 * (505 - PotSenValue);
    digitalWrite(LedPin, HIGH);
    delay(a);
    digitalWrite(LedPin, LOW);
    delay(a);  
  }
  
  else if (PotSenValue > 515) {
    int b = 0.5 * (PotSenValue - 505);
    digitalWrite(LedPin, HIGH);
    delay(b);
    digitalWrite(LedPin, LOW);
    delay(b);
  }
   
  else if ( (PotSenValue > 505) && (PotSenValue < 515) ) { //This is the part where the button will end the loop upon press
      digitalWrite(LedPin, LOW);
      ButtonState = digitalRead(Button);
      if (ButtonState == HIGH) {
        int x = 1;
      } else {
        delay(100);
    }
  }
 
  delay(100);
  
  } //End of the While loop
  
//The rest of the code will repeat here forever
int x = 0;

There you already declare a variable x of type int.

      ButtonState = digitalRead(Button);
      if (ButtonState == HIGH) {
        int x = 1;
      } else {
        delay(100);

And now, you are re-declaring it. Remove int from x.

Also, your code does not compile? Did you miss one more curly braces?

Awesome thanks. I sill compiles, but I will remove the second 'int' tonight.

If that doesn't close the while loop I am thinking of perhaps replacing the x = 1; with a break; statement and declaring x = 1 outside the loop. I'll work on it tonight and keep you posted. Thanks for the advice.

I sill compiles, but I will remove the second 'int' tonight.

Your code as it is compiles OK because the two x variables that you declare have different scopes, so it is valid code but is not doing what you want.

Google "C variable scope" for more information.