Why if‑else and loops matter in Java
Ever wondered how a game knows when you win or how a calculator repeats a calculation until you hit "="? That’s the magic of if‑else statements and loops – the brain’s way of making choices and doing things over and over.
💡 In Simple Words: An if‑else checks a condition and decides which block of code to run. A loop repeats a block of code while a condition stays true. Think of a traffic light: if it’s green, go; else, wait. And a loop is like a music playlist that keeps playing songs until you hit stop.
What is an if‑else statement?
An if‑else statement is a control structure that lets your program choose between two paths. The word condition means any expression that evaluates to true or false – like score > 50. If the condition is true, the code after if runs; otherwise, the code after else runs.
int score = 68;
if (score > 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}Here, score > 50 is the condition. Because 68 is bigger than 50, the program prints "Pass".
How loops work in Java
A loop is a way to repeat a set of statements. The word iteration is just a fancy term for one round of the loop. Java gives us three main kinds of loops: for, while, and do‑while. All of them keep checking a condition; as long as it stays true, the loop keeps running.
1. The for loop
Use a for loop when you know exactly how many times you want to repeat something. It has three parts inside the parentheses: initialization, condition, and update.
for (int i = 1; i This prints numbers 1 through 5. The variable i starts at 1, the loop runs while i is true, and i++ adds one after each round.
2. The while loop
A while loop is perfect when you don’t know the exact count ahead of time. It checks the condition before each iteration.
int n = 1;
while (n This prints 1, 3, 5, 7, 9. The loop stops as soon as n becomes 10 or more.
3. The do‑while loop
The do‑while loop guarantees the code runs at least once because it checks the condition after the block.
int count = 0;
do {
System.out.println("Hello " + count);
count++;
} while (count It prints "Hello 0", "Hello 1", and "Hello 2".
When to choose if‑else vs. loops
If you need to pick one action out of two (or more) based on a single test, reach for if‑else. If you need to repeat an action many times, use a loop. Sometimes you combine them – a loop that contains an if‑else to handle special cases inside each iteration.
Comparison of Java loops
| Loop Type | Best For | Check Timing |
|---|---|---|
for | Known number of repetitions | Before each iteration |
while | Unknown count, condition‑driven | Before each iteration |
do‑while | Must run at least once | After each iteration |
Quick cheat sheet
- Use
if (cond) { … } else { … }for two‑way decisions. - Chain multiple
else iffor more than two choices. - Prefer
forwhen you can count iterations. - Pick
whilewhen the loop ends on a condition you don’t know in advance. - Choose
do‑whilewhen the body must execute at least once.
Flow of an if‑else decision
📝 Likely Exam Questions
1. Write a Java program that prints "Even" for even numbers and "Odd" for odd numbers between 1 and 10 using an if‑else statement.
Model answer:
for (int i = 1; i 2. Explain the difference between a while loop and a do‑while loop with an example.
Model answer: A while loop checks its condition before running the body, so if the condition is false initially the body never runs. A do‑while loop checks after the body, guaranteeing at least one execution. Example shown in the article.
3. When would you prefer a for loop over a while loop?
Model answer: When the number of repetitions is known beforehand, because the for loop keeps initialization, condition, and update in one line, making the code cleaner.
4. What will be the output of the following code?
int x = 5;
if (x > 3) {
if (x Model answer: The program prints "A" because 5 is greater than 3 and also less than 7.5. Convert a while loop that prints numbers 1‑5 into an equivalent for loop.
Model answer:
for (int i = 1; i