Tuesday, June 14, 2022

Java do-while Loop With Examples

In Java programming language there are three types of loops- do-while loop, while loop, and for loop. In this post we’ll learn about do-while loop in Java along with usage examples.

do-while loop in Java

A do-while loop in Java repeatedly executes a statement or a block of statements while the given condition is true. Java do-while loop is similar to while loop except that the condition in do-while loop is evaluated after the loop body is executed.

Syntax of the do-while loop in Java is as follows-

 
do {
  // loop body
} while (condition);

In do-while loop condition that controls the loop is at the bottom of the loop so the loop always executes at least once where as in while loop if condition evaluates to false at the first time itself then the loop body is not executed at all.

Java do while loop execution flow

Java do while loop examples

1- Using do-while loop to print numbers 1..5.

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

Output

 
1
2
3
4
5

2- Since do-while loop is executed at least once so this loop is a good choice if you want to present a simple menu to the user, because you will definitely want menu to be displayed at least once.

public class DoWhileDemo {
  public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
    int choice;
    do {
      System.out.println("----Language Menu----: ");
      System.out.println(" 1. Java");
      System.out.println(" 2. Python");
      System.out.println(" 3. C#");
      System.out.print("Enter your preferred language (1-3): ");
      choice = sc.nextInt();
      //while choice is not between 1..3 be in the loop
    } while( choice < 1 || choice > 3);
    sc.close();
    switch(choice) {
      case 1:
        System.out.println("Preferred Language- Java");            
        break;
      case 2:
        System.out.println("Preferred Language- Python");
        break;
      case 3:
        System.out.println("Preferred Language- C#");
        break;
    }
  }
}

Output

 
----Language Menu----: 
 1. Java
 2. Python
 3. C#
Enter your preferred language (1-3): 5
----Language Menu----: 
 1. Java
 2. Python
 3. C#
Enter your preferred language (1-3): 1
Preferred Language- Java

As you can see when 5 is entered, loop is repeated only when the choice is between 1..3 loop is terminated.

That's all for this topic Java do-while Loop With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Arithmetic And Unary Operators in Java
  2. if else Statement in Java With Examples
  3. Type Wrapper Classes in Java
  4. static Keyword in Java With Examples
  5. Array in Java With Examples

You may also like-

  1. try-catch Block in Java Exception Handling
  2. Type Casting in Java With Conversion Examples
  3. ArrayList in Java With Examples
  4. How HashSet Works Internally in Java
  5. AtomicLong in Java With Examples
  6. Python First Program - Hello World
  7. Spring Bean Life Cycle
  8. What is Big Data

No comments:

Post a Comment