KEMBAR78
Five Do While Loop Example Java | PDF | Control Flow | Computer Engineering
0% found this document useful (0 votes)
10 views4 pages

Five Do While Loop Example Java

Uploaded by

Tesu Rathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Five Do While Loop Example Java

Uploaded by

Tesu Rathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

A do-while loop is a control flow statement in Java that executes a block of code at least once,

and then repeatedly executes the loop as long as a given condition is true. The condition is
checked at the end of the loop, which guarantees that the code inside the loop body runs at
least one time.

1. Basic Iteration
This example shows a simple counter that prints numbers from 1 to 5. Even if the starting value
was greater than 5, the loop body would still execute once.
public class BasicLoop {​
public static void main(String[] args) {​
int i = 1;​
do {​
System.out.println(i);​
i++;​
} while (i <= 5);​
}​
}​

Output:
1​
2​
3​
4​
5​

2. User Input Validation


This is a very common and practical use of a do-while loop. The loop prompts the user for input
and continues to do so until a valid entry is provided. This is ideal because you need to get the
input before you can check if it's valid.
import java.util.Scanner;​

public class InputValidation {​
public static void main(String[] args) {​
Scanner input = new Scanner(System.in);​
int number;​
do {​
System.out.print("Enter a positive number: ");​
number = input.nextInt();​
} while (number <= 0);​
System.out.println("You entered: " + number);​
input.close();​
}​
}​

Output:
Enter a positive number: -10​
Enter a positive number: 0​
Enter a positive number: 5​
You entered: 5​

3. Implementing a Menu System


A do-while loop is perfect for menu-driven applications. The menu must be displayed to the user
at least once, and the loop continues as long as the user hasn't chosen the "exit" option.
import java.util.Scanner;​

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

do {​
System.out.println("--- Main Menu ---");​
System.out.println("1. Play Game");​
System.out.println("2. View Scores");​
System.out.println("3. Exit");​
System.out.print("Enter your choice: ");​
choice = scanner.nextInt();​

if (choice == 1) {​
System.out.println("Playing game...");​
} else if (choice == 2) {​
System.out.println("Viewing scores...");​
}​
} while (choice != 3);​

System.out.println("Exiting program. Goodbye!");​
scanner.close();​
}​
}​

Output:
--- Main Menu ---​
1. Play Game​
2. View Scores​
3. Exit​
Enter your choice: 1​
Playing game...​
--- Main Menu ---​
1. Play Game​
2. View Scores​
3. Exit​
Enter your choice: 3​
Exiting program. Goodbye!​

4. Simulating a Dice Roll


This example simulates rolling a die and continues to roll until a specific number, like 6, is rolled.
The loop executes at least once because the first roll must happen before the condition can be
checked.
import java.util.Random;​

public class DiceRoll {​
public static void main(String[] args) {​
Random rand = new Random();​
int roll;​
do {​
roll = rand.nextInt(6) + 1; // Rolls a number from 1 to 6​
System.out.println("You rolled a " + roll);​
} while (roll != 6);​
System.out.println("You rolled a 6! The loop has ended.");​
}​
}​

Output (example):
You rolled a 2​
You rolled a 4​
You rolled a 6​
You rolled a 6! The loop has ended.​

5. Loop with a break Statement


While do-while loops typically rely on their condition, you can also use a break statement to exit
the loop prematurely from within the loop body. This is useful for handling special conditions.
public class DoWhileBreak {​
public static void main(String[] args) {​
int sum = 0;​
int i = 1;​
do {​
sum += i;​
System.out.println("Current sum: " + sum);​
if (sum >= 10) {​
System.out.println("Sum has reached or exceeded 10.
Breaking loop.");​
break;​
}​
i++;​
} while (true); // An infinite loop that will be broken out of​
}​
}​

Output:
Current sum: 1​
Current sum: 3​
Current sum: 6​
Current sum: 10​
Sum has reached or exceeded 10. Breaking loop.​

You might also like