As you have seen, an if-else statement checks a Boolean condition only once. However, sometimes you want to keep checking the condition as long as it is true. To do this you can use a while loop. A while loop repeats the statements inside it as long as a Boolean expression is true.
As an example, consider the code below. This code will make the Finch beak red for as long as the value of the distance sensor is less than 30 cm. The keyword while is followed by a Boolean expression, bird.getDistance() < 30. If this expression is true, the program runs the statement inside the loop and turns the beak red. Then it checks the Boolean expression again. If it is still true, the program turns the beak red again, so you will see it stay red. The loop continues repeating the statement inside it until it checks the Boolean expression and finds that it is false. Then the program skips the indented statement inside the loop and moves on to the next unindented line of code. In this case, the program turns the beak green for one second and then off.