- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java do Keyword
Usage and Examples
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. Iffalse
, 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
- 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
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) |