"Hi."

"Hi, Ellie!"

"It's time to learn about loops. Loops are as simple as if/else statements, but even more interesting. You can use a loop to execute any command or a block of commands multiple times. In general, a loop looks like this:"

Loop (example 1)

while(boolean condition)               
    command;
Loop (example 2)

while(boolean condition) 
    block of commands in curly brackets

"It's all very simple. A command or block is executed again and again as long as the loop condition is true. First, the condition is checked. If the condition is true, the loop body (block of commands) is executed. The condition is checked again. If the condition is true, the loop body is executed again. This repeats until the condition ceases to be true."

"What if it is always true or always false?"

"If it is always true, then the program will never stop running: it will repeat the loop indefinitely. If it's always false, then the loop body will never be executed."

Here are some examples:

Java code Description
int i = 3;
while (i >= 0)
{
    System.out.println(i);
    i--;    //Decrease by 1
}
3
2
1
0
int i = 0;
while (i < 3)
{
    System.out.println(i);
    i++;   //Increase by 1
}
0
1
2
boolean isExit = false;
while (!isExit)
{
    String s = buffer.readLine();
    isExit = s.equals("exit");
}
The program will print strings from the keyboard until the string 'exit' is input.
while (true) 
    System.out.println("C");
The program will repeatedly display the letter C on screen.
while (true) 
{
    String s = buffer.readLine();
    if (s.equals("exit")) 
        break;
}
The program will read strings from the keyboard until the string 'exit' is input.

"After conditional statements, this doesn't seem complicated. I already want to try it."