Ever wondered how a video game decides whether you win or lose, or how a robot repeats a task until it's done?
💡 In Simple Words: An if‑else statement checks a condition and picks one of two paths, while a loop tells Java to run the same code over and over until a rule says stop.
What is an if‑else statement?
Think of an if‑else like a traffic light. If the light is green, you go; else (if it’s red) you stop. In Java, the “light” is a condition – a true/false question.
Syntax of if‑else in Java
Here’s the basic shape:
if (condition) {
// code when condition is true
} else {
// code when condition is false
}
Example: decide if a number is even.
int n = 7;
if (n % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
n % 2 == 0 asks “does n divided by 2 leave no remainder?” If yes, the first block runs; otherwise the else block runs.
More if‑else flavours
- if‑else if: chain several conditions one after another.
- Nested if: put an if‑else inside another if‑else.
Example of if‑else if:
int score = 85;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'D';
}
Notice how each test is tried only if the previous one failed. That saves time, just like a librarian checks the most popular books first.
Loops – making Java repeat work
A loop is a way to tell Java “keep doing this until I say stop”. Imagine a washing machine: it spins, rinses, spins again, and stops when the timer hits zero. The same idea works in code.
while loop
Structure:
while (condition) {
// code to repeat
}
The condition is tested first. If it’s false right away, the block never runs.
Example – print numbers 1 to 5:
int i = 1;
while (i
do‑while loop
Difference: the block runs **once** before the condition is checked.
do {
// code
} while (condition);
Example – ask a user for a positive number, keep asking until they comply:
int num;
do {
System.out.print("Enter a positive number: ");
num = scanner.nextInt();
} while (num
for loop
Best when you know how many times you want to repeat.
for (initialisation; condition; update) {
// code
}
Example – same 1‑to‑5 printing:
for (int i = 1; i
Quick comparison of Java loops
| Loop | When condition is checked | Typical use |
|---|---|---|
| while | Before each iteration | Unknown number of repeats, may never run |
| do‑while | After first iteration | Must run at least once (e.g., menu display) |
| for | Before each iteration | Fixed count, easy counter handling |
Tips to avoid common mistakes
- Never forget the curly braces { } when you have more than one statement inside if or loop.
- Remember
==compares values, while=assigns a value. - Watch out for infinite loops – make sure the condition will eventually become false.
📝 Likely Exam Questions
- Write a Java program to print all odd numbers between 1 and 20 using a for loop.
for (int i = 1; i - Explain the difference between while and do‑while loops with an example.
Answer: while checks the condition before the first execution, so the block may never run. do‑while runs the block once and then checks, guaranteeing at least one execution. Example code shown above. - What will be the output of the following code?
Answer: It printsint x = 10; if (x > 5) { if (xAbecause both conditions are true. - Convert the while loop below into an equivalent for loop.
Answer:int i = 0; while (ifor (int i = 0; i - Why is it important to update the loop variable inside a while loop?
Answer: Without updating, the condition never changes and the loop becomes infinite, causing the program to hang.