Java Reference Library →

do Keyword Usage and Examples

do Keyword Usage and Examples

Learn how to use the 'do' keyword in Java to implement do-while loops. Understand its syntax, behavior, and practical examples for beginners.

This reference explains do Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

do Keyword in Java

In Java, the do keyword is part of the do-while loop, a control flow statement that allows a block of code to execute at least once, and then repeatedly based on a given condition.

This structure is unique because it checks the loop condition after executing the loop body. That means even if the condition is false at the start, the loop will run once — guaranteed.

Syntax of the do Keyword

do {
  // statements
} while (condition);

How It Works

  • The block inside do { } executes first.
  • Then the condition is evaluated in the while clause.
  • If the condition is true, the loop runs again. If false, the loop ends.

Example 1: Basic do-while Loop

This example prints numbers from 1 to 5 using the do keyword.

public class DoWhileExample {
  public static void main(String[] args) {
    int i = 1;
    do {
      System.out.println("Number: " + i);
      i++;
    } while (i <= 5);
  }
}
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Explanation

Java · example discussed here

public class DoWhileExample {
  public static void main(String[] args) {
    int i = 1;
    do {
      System.out.println("Number: " + i);
      i++;
    } while (i <= 5);
  }
}

- The variable i starts at 1.
- The loop runs and prints the number.
- After printing, i is incremented.
- The condition (i <= 5) is checked after the block executes.

Example 2: do-while Runs Even If Condition Is False

Let’s see what happens when the condition is false to begin with.

public class DoWhileOnce {
  public static void main(String[] args) {
    int i = 10;
    do {
      System.out.println("This will run even if condition is false!");
    } while (i < 5);
  }
}
This will run even if condition is false!

Explanation

Java · example discussed here

public class DoWhileOnce {
  public static void main(String[] args) {
    int i = 10;
    do {
      System.out.println("This will run even if condition is false!");
    } while (i < 5);
  }
}

Although i is 10 and the condition i < 5 is clearly false, the loop body runs once before the condition is checked. That’s the defining behavior of a do-while loop.

Common Use Cases

  • Input validation (e.g., ask user input until valid)
  • Menu-driven programs that require at least one interaction
  • Games or simulations where the first step must always be executed

Example 3: Input Until User Types 'exit'

This example keeps asking for input until the user types 'exit'.

import java.util.Scanner;

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

    do {
      System.out.print("Enter a command (type 'exit' to quit): ");
      input = scanner.nextLine();
    } while (!input.equalsIgnoreCase("exit"));

    System.out.println("Program ended.");
  }
}
Enter a command (type 'exit' to quit): hello
Enter a command (type 'exit' to quit): test
Enter a command (type 'exit' to quit): exit
Program ended.

Best Practices

  • Use do-while when the block must execute at least once.
  • Be cautious with conditions to avoid infinite loops.
  • Keep logic simple and avoid deeply nested do-while loops.

Comparison with while Loop

Feature while do-while
Condition checked Before executing block After executing block
Guaranteed execution? No Yes (at least once)

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →