Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

While Loops

While-loops are similar to for loops in that they help you repeat an action or set of statements.

while loop executes the body of the loop as long as (or while) a Boolean condition is true. When the condition is false, we exit the loop and continue with the statements that are after the body of the while loop. If the condition is false the first time you check it, the body of the loop will not execute.

Notice the while statement looks a lot like an if statement, but it runs more than once. The curly brackets { } are optional when there is just 1 statement following the condition, but required if there is more than 1 statement in the loop. We would recommend that you always use curly brackets even if you only have 1 statement to run.

// if statements just run once if the condition is true
if (condition)
{
    statements;
}

// while statements are repeated while the condition is true
while (condition)
{
  statements;
}

The simplest loops are counter-controlled loops like below, where the loop control variable is a counter that controls how many times to repeat the loop. There are 3 steps to writing a loop using this loop control variable as seen below in a loop that counts from 1 to 10.

Java doesn’t require your code to be correctly indented (code moved to the right a few spaces) to make it clear what statements are part of the body of the loop, but it is standard practice to do so.

While loops are very similar to for loops. Generally, you want to use a while loop when you don’t know how many items/times you will be looping. You will use a for-loop when you know exactly how many times/items you will be looping. They can each do what the other does though. See their similarities below:

Input-Controller Loops

You can use a while loop to repeat the body of the loop a certain number of times as shown above. However, a while loop is typically used when you don’t know how many times the loop will execute. It is often used for an input-controlled loop where the user’s input indicates when to stop. For example, in the code below, the while loop stops when you type in “Bye”. The stopping value is often called the sentinel value for the loop. Notice that if you type in “Bye” right away, the loop will never run. If the loop condition evaluates to false initially, the loop body is not executed at all. Another way to stop the loop prematurely is to put in a return statement that makes it immediately return from the method.

Loops Debugging

A very important skill to develop is the ability to trace the value of different variables in a loop. This helps with debugging a program that is not working properly or to plan a program before writing the code. Watch the following video as the author works through a while loop line by line.