Do While-Loop in Java

by Jasleen Chhabra | Updated on 24 August 2024

Table of Contents (Click any Topic to view first)

  1.  Syntax of the do-while Loop
  2. Example 1: Simple do-while Loop
  3. Example 2: Using do-while Loop for User Input
  4. Example 3: do-while Loop with Conditional Exit

Understanding the Do While-Loop in Java

In Java programming, the do-while loop is a variation of the while loop that ensures the execution of its block of code at least once, regardless of the loop condition. It's useful when you want to execute a block of code and then check the condition for further iterations. Let's explore the do-while loop in Java with detailed explanations and examples.

Syntax of the do-while Loop:

do {

    // Code to be executed

} while (condition);

The do-while loop first executes the block of code inside it, and then evaluates the loop condition. If the condition is true, the loop continues to execute. The loop terminates when the condition becomes false.

Example 1: Simple do-while Loop

public class SimpleDoWhileLoop {

    public static void main(String[] args) {

        int i = 1;

        do {

            System.out.println("Iteration " + i);

            i++;

        } while (i <= 5);

    }

}

In this example, the do-while loop iterates from 1 to 5, printing each iteration number. Since the condition (i <= 5) is evaluated after the first execution, the loop executes at least once even if the condition is initially false.

Example 2: Using do-while Loop for User Input

import java.util.Scanner;

 

public class DoWhileLoopUserInput {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String userInput;

        do {

            System.out.print("Enter 'exit' to quit: ");

            userInput = scanner.nextLine();

            System.out.println("You entered: " + userInput);

        } while (!userInput.equals("exit"));

        scanner.close();

    }

}

This example demonstrates the use of a do-while loop for continuous user input. The loop prompts the user to enter text and prints the input until the user inputs "exit," at which point the loop terminates.

Example 3: do-while Loop with Conditional Exit

public class ConditionalExitDoWhileLoop {

    public static void main(String[] args) {

        int i = 1;

        do {

            System.out.println("Iteration " + i);

            if (i == 5) {

                break; // Exit the loop when i reaches 5

            }

            i++;

        } while (i <= 10);

    }

}

In this example, the loop iterates from 1 to 10, but it exits prematurely when the value of i becomes 5. The do-while loop guarantees the execution of the loop body at least once before checking the loop condition.

Conclusion:

The do-while loop in Java ensures the execution of its block of code at least once, making it suitable for scenarios where you need guaranteed execution before evaluating the loop condition. Whether handling user input, implementing conditional exits, or iterating through a range of values, the do-while loop offers flexibility and reliability in loop constructs.

 


FAQ

Any Questions?
Look Here.

Related Articles

Abstraction in Java

Binding in Java

Break Statement in Java

Classes & Objects in Java

Collections Framework in Java

Comments in Java

Continue Statement in Java

Control Statements in Java

Data Types in Java

Encapsulation in Java

Exception Handling in Java

For-Loop In Java

Hello World Program in Java

If-else Statement in Java

Inheritance in Java

Introduction to Java

Java Database Connectivity (JDBC)

Java Development Tools and Frameworks

Java GUI (Graphical User Interface) Programming

Java I/O

Java Vs. C++

Methods and Constructors in Java

Multithreading in Java

Object Oriented Programming in Java

Operators in Java

Polymorphism in Java

Scanner Class in Java

Setting Up Java Environment

Static Keyword in Java

Super Keyword in Java

Switch Statement in Java

This Keyword in Java

Variables in Java

While-Loop in Java