Control Flow Lesson 12 of 21
Java while and do-while Loops
Repeat code with while and do-while loops — counters, savings goals, input validation, menus — and avoid infinite loops.
A loop repeats code. Java has three loops: while, do-while and for. This lesson covers the first two, which are best when you don't know exactly how many times you'll repeat.
while
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
System.out.println("Done!");
}
}
Output
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 Done!
Before each repetition, Java checks the condition. When i becomes 6, i <= 5 is false and the loop ends.
Reaching a goal
public class Main {
public static void main(String[] args) {
double savings = 0;
int weeks = 0;
while (savings < 2500) {
savings += 300;
weeks++;
}
System.out.println("Weeks needed: " + weeks);
System.out.println("Saved: ₱" + savings);
}
}
Output
Weeks needed: 9 Saved: ₱2700.0
Digits of a number
public class Main {
public static void main(String[] args) {
int n = 9075;
int sum = 0;
int reversed = 0;
while (n > 0) {
int digit = n % 10;
sum += digit;
reversed = reversed * 10 + digit;
n /= 10;
}
System.out.println("Sum of digits: " + sum);
System.out.println("Reversed: " + reversed);
}
}
Output
Sum of digits: 21 Reversed: 5709
n % 10 gets the last digit, and n /= 10 removes it. This digit-by-digit pattern appears often in programming exercises and contests.
do-while: run at least once
A do-while loop checks the condition after each run, so the body always runs at least once. It's ideal for menus and input checking:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int grade;
do {
System.out.print("Enter a grade (0-100): ");
grade = sc.nextInt();
if (grade < 0 || grade > 100) {
System.out.println("Invalid, try again.");
}
} while (grade < 0 || grade > 100);
System.out.println("Saved: " + grade);
}
}
Output
Enter a grade (0-100): 105 Invalid, try again. Enter a grade (0-100): 89 Saved: 89
A menu program
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double balance = 1000;
int choice;
do {
System.out.println("\n[1] Balance [2] Deposit [3] Withdraw [0] Exit");
System.out.print("Choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> System.out.println("Balance: ₱" + balance);
case 2 -> {
System.out.print("Amount: ");
balance += sc.nextDouble();
}
case 3 -> {
System.out.print("Amount: ");
double amt = sc.nextDouble();
if (amt > balance) System.out.println("Insufficient balance.");
else balance -= amt;
}
case 0 -> System.out.println("Thank you!");
default -> System.out.println("Invalid choice.");
}
} while (choice != 0);
}
}
Output
[1] Balance [2] Deposit [3] Withdraw [0] Exit Choice: 3 Amount: 1500 Insufficient balance. [1] Balance [2] Deposit [3] Withdraw [0] Exit Choice: 0 Thank you!
This "ATM menu" structure, a do-while around a switch, is a classic practical exam program in Java classes.
break and continue
breakexits the loop immediately.continueskips the rest of this repetition and checks the condition again.
public class Main {
public static void main(String[] args) {
int n = 0;
while (true) {
n++;
if (n % 2 == 0) continue; // skip even numbers
if (n > 9) break; // stop after 9
System.out.print(n + " ");
}
System.out.println();
}
}
Output
1 3 5 7 9
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04