# While Loop

A while loop repeats a block of code for as long as its condition is true, which suits jobs where you don't know the number of repeats in advance.

Novice · Java · Updated November 16, 2026

By Gregor Dzierzon

Loops are often used to process all elements in an array, but they can do more than that. We use loops to repeatedly execute a set of statements until a certain condition is met.

![Flowchart of a loop: while the decision is yes, the action runs and returns to the decision; when it is no, the loop ends](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/09/68ac1e8b680d4dafb96d2cf7b35d6d4e.png)

There are four primary types of loops in Java, each with its own use case: `while`, `do-while`, `for`, and `for-each`.

The `while` loop executes a block of code as long as a specified condition is true. This type of loop is particularly useful when the number of iterations is not known beforehand.

## Example

This program loops indefinitely as long as the user keeps entering "yes".

```java
import java.util.Scanner;

public class WhileLoopExample
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Do you want to run the program? (yes/no)");
        String input = scanner.nextLine();

        while (input.equalsIgnoreCase("yes"))
        {
            System.out.println("Inside the loop.");

            System.out.println("Do you want to go again? (yes/no)");
            input = scanner.nextLine();
        }
        System.out.println("Loop ended.");
    }

}
```

In this example, the loop will continue as long as the user inputs "yes". The `equalsIgnoreCase` method is used to handle case sensitivity.

## Do it: count the lunches your savings can buy

Your savings pay for lunch at the deli, one lunch at a time, until there isn't enough left for another. Write a function that works out how many lunches that is.

The function gets two values:

- `savings`: the money you have now, a `double` (`50.0`).
- `lunchPrice`: what one lunch costs, a `double` (`12.0`). It's always more than 0.

For example, with $50 saved and $12 lunches:

| Lunch | Savings before it | Enough for it? | Savings after it |
|---|---|---|---|
| 1 | $50 | Yes | $38 |
| 2 | $38 | Yes | $26 |
| 3 | $26 | Yes | $14 |
| 4 | $14 | Yes | $2 |
| 5 | $2 | No: stop | |

So `lunchesYouCanBuy(50.0, 12.0)` returns `4`.

**Your job:** write the function `int lunchesYouCanBuy(double savings, double lunchPrice)` with a `while` loop.

- While `savings` is enough for one more lunch, buy it: take `lunchPrice` out of `savings`, and count the lunch.
- Return how many lunches you bought.
- Having exactly the price is enough: $36 saved and $12 lunches buys 3.
- When `savings` is less than one lunch's price, return `0`.

Practiced live on the lesson page: write the Java right there and check it.

## Cheat sheets

- [While and Do-While Loops](https://www.ask-codey.com/cheat-sheets/looping-while-loop-cheat-sheet)

Source: https://www.ask-codey.com/lessons/looping-while-loop
