While loop stoping my whole program

I am writing a code that first sets the main reference and then measures again. If the current measurement is within + or -4 of the set value then the servo arm moves. If it is not in the specified range then the ultrasonic sensor is supposed to keep measuring until it is within the range, and then move the servo arm. I am at a point where it runs when it is within the range but once the measured value goes out of the +-4 range it stops running and the ultrasonic sensor stops checking the distance (I know this because the serial monitor stops giving the values of the distance).

This is the code that I have

void loop() {
  // put your main code here, to run repeatedly:

  //sets the constant to compare all other values to
  const int setDistance = measureDistance();

  int x = 0;
  //the for loop runs through the loop 5 times before it repeats
  for (x = 0; x < 5; x++) {
    int currentDistance = measureDistance();

    //This while loop is supposed to repeat untill the part is at the right location which is + or - 4 from the set point
    while (currentDistance > setDistance + 4 || currentDistance < setDistance - 4); {
      int currentDistance = measureDistance();
      delay(500);
    }

    //after knowing that its in the right place turns the servos
    servoLeft.write(45);
    delay(1000);
    servoLeft.write(135);
    delay(1000);
  }
}

int measureDistance() {
  //subroutine that measures the machine's location
}

Can anybody figure out why it is not updating when the while loop should be running (when the current value is out of the range)? Any help would be useful.

I would replace the WHILE with IF and allow loop() to do the repetition.

...R

|| and && bind lower than arithmetic operations, no ambiguity there, paratheses are just clutter in this example.

The operators to be careful with are & and |.

Thanks for informing me about the semicolon and about trying to use a for loop. I tried changing it to a for loop and now it won't stop running. This is the void loop section that I currently have. Any ideas as to why it won't stop running or other ways I could do it?

  const int setDistance = measureDistance();
  Serial.print("set distance: ");
  Serial.println(setDistance);
  int x = 0;
  for (x = 0; x < 5; x++) {

    servoLeft.write(45);
    delay(1000);

    servoLeft.write(100);
    delay(1000);

    int currentDistance = measureDistance();

    for (currentDistance > setDistance + 4 || currentDistance < setDistance - 4; currentDistance < setDistance + 4 && currentDistance > setDistance - 4;) {
      int currentDistance = measureDistance();
      delay(500);
    }

  }

You have the second FOR loop inside the first one. Is that intentional?

...R

I think i am going to stick with the while loop. I wrote another code to test out the while loop. The code also gets stuck in the while loop.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  int x = 4;
  int y = random(0, 8);
  Serial.println(y);
  while (y > x) {
    int y = random(0, 8);
    Serial.println(y);
    delay(3000);
  }
  delay(1000);
}

The code is supposed to display random numbers every second but if it is greater than 4 it picks another number and delays 3 seconds. when I run the code and it goes into the while loop it never comes back out of it.

I kept playing around with the code and I found out that the reason the code kept messing up is because the declaration of the value that changes is an int once you remove int the code works like it is supposed to.