While Loops

Loops are used in programming to repeat the execution of code instead of writing the line code of over and over again.

A Code Example

int countdown = 5; 
while (countdown >= 0) {
    System.out.println(countdown);
    countdown--;
}

Line by Line Analysis

int countdown = 5

We have set an outer variable to help us with our while loop.

while (countdown >= 0)

While is a keyword in java.

It helps us start a looping structure.

Much like an if statement a while loop will execute its code block when its boolean expression is true.

Unlike an if statement, a while loop will continue to execute its code block as long as the boolean expression is true.

countdown--;

This line of code just decrements countdown variable by 1 at every iteration.

Formalization

Iterations: instructions or code being repeated until a specific end result is achieved.

While Loop Format in Java:

while (condition) {
    // code to be executed repeatedly
}

Infinite Loop: An infinite loop is a while loop that never ends because the boolean condition of the loop never turns to false.

Example:

String value = "Hello";
while (value.equals("Hello")) {
    System.out.println("Hello");
}

Using While Loops as a Counter

int counter = 0;
while (counter <= 10) {
    System.out.println("The counter is at: " + counter);
    counter++; // Incremental operator to increase a variable by 1.
}

By using some sort of a counting variable, you get to control the number of iterations you execute.

Using a boolean variable to control when to end a while loop

Scanner in = new Scanner(System.in);
String user_input = ""; // Declare an empty string
boolean loop = true;

while (loop == true) {
    System.out.println("Hello, World!");

    // Loop exiting code
    System.out.println("Would you like to end the loop? (Yes/No).");
    user_input = in.nextLine();
    if (user_input.equals("Yes")) {
        loop = false;
    }
}

We can also take an user input every iteration to check if they want to exit to prevent an infinite loop.

Last updated