Java do Keyword

The do keyword in Java is used to create a loop that will execute a block of code at least once, and then repeatedly execute the block as long as a specified condition is true. This type of loop is called a do-while loop.

Table of Contents

  1. Introduction
  2. do-while Keyword Syntax
  3. Understanding do-while
  4. Examples
    • Basic Usage
    • Using do-while to Read User Input
    • Using do-while with Arrays
  5. Real-World Use Case
  6. Conclusion

Introduction

The do-while loop in Java allows you to execute a block of code at least once, and then continue executing it based on a condition. This is useful when you need the loop body to execute at least one time regardless of the condition.

do-while Keyword Syntax

The syntax for the do-while loop is as follows:

do {
    // code to be executed
} while (condition);

Example:

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 10);

Understanding do-while

The do-while loop is similar to the while loop, but with one key difference: the condition is evaluated after the loop body has executed. This guarantees that the loop body will execute at least once.

Key Points:

  • The condition is checked after the loop body is executed.
  • The loop body is guaranteed to execute at least once.
  • The condition must be a boolean expression (returns true or false).

Examples

Basic Usage

To demonstrate the basic usage of the do keyword, we will print numbers from 0 to 9.

Example

public class DoWhileExample {
    public static void main(String[] args) {
        int i = 0;

        do {
            System.out.println(i);
            i++;
        } while (i < 10);
    }
}

Using do-while to Read User Input

The do-while loop is often used to read user input until a specific condition is met.

Example

import java.util.Scanner;

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

        do {
            System.out.println("Enter something (type 'exit' to quit): ");
            input = scanner.nextLine();
            System.out.println("You entered: " + input);
        } while (!input.equalsIgnoreCase("exit"));

        scanner.close();
    }
}

Using do-while with Arrays

You can use the do-while loop to iterate over elements in an array.

Example

public class DoWhileArrayExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        int i = 0;

        do {
            System.out.println("Element at index " + i + ": " + numbers[i]);
            i++;
        } while (i < numbers.length);
    }
}

Real-World Use Case

Repeatedly Prompting User Until Valid Input

In real-world applications, the do-while loop is useful for repeatedly prompting the user until they provide valid input.

Example

import java.util.Scanner;

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

        do {
            System.out.println("Enter a positive number: ");
            while (!scanner.hasNextInt()) {
                System.out.println("That's not a number!");
                scanner.next(); // discard the invalid input
            }
            number = scanner.nextInt();
        } while (number <= 0);

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

        scanner.close();
    }
}

Conclusion

The do keyword in Java, used in the do-while loop, is used for scenarios where you need a block of code to execute at least once before checking a condition. By understanding and using the do-while loop, you can handle situations that require at least one execution of the loop body before condition evaluation, making your Java programs more dynamic and responsive to user input and other conditions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top