“Those who cannot remember the past are condemned to repeat it.” George Santayana
In programming, there are times that we want to repeat the execution of a block of code, until a condition is met.
For example let’s say we have a char variable named alpha and the initial value of this variable is a. There’s a block of code that if we run it, it will increase the value of this variable by one. So every time we run that block of code, the value of this alpha variable changes to the next alphabet character (b, c, d and e etc.).
What we want here is a loop that can repeat the execution of that block until the value of the alpha variable reaches the value z.
This is a candidate that we can use the while loop statement.
Before we write down and run the example above, let’s see the structure of while loop.
Java while syntax
while (condition) {
// the code block to be executed
}
`while keyword`: To create a while loop we start with the keyword `while`.
( ): The parentheses that come after the while keyword, is used to set the condition of the loop. The condition here can be like while the value of a variable is not equal to something, repeat the loop.
Note: as long as the result of the condition is true, the body of the while loop will run.
Also the true and false values can be used as the value of the condition. Of course in such case, the value true will cause the loop to repeat itself indefinitely (unless we use statements like break; or return; in the body of the loop to stop it at some point). Also if we put the value false as the condition of the loop , the body will not run at all because the condition is already false!
`{ }`: The body of the while loop starts from the opening brace `{` and ends right at the closing brace `}`.
Note: every time the engine executes the body, the condition will be checked again.
Java while loop Example:
public class Simple {
public static void main(String[] args) {
char character = 'a';
while (character != 'z'){
System.out.print(character+", ");
character++;
}
}
}
Output:
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y,
As mentioned in the data-type section, char is another integer data-type and so it is possible to apply the ++ operator on a variable of this type.
In this example the value of the character variable is first set to a and next we use this variable as the condition in the while loop. Basically the condition of the loop is to check the value of the character variable and as long as the value is not equal to z, the body of the loop should be executed.
In another word, until the value of the character variable reaches the value z, the condition of the while loop is true and so the body of that loop will run over and over again.
Note: in the body of the loop we used ++ operator to increase the value of the character variable.
Basically what we did here is to ensure the loop will not run indefinitely.
This is an important note to remember: Design the body of a loop in a way to affect the condition of the loop so it doesn’t run forever!