Why if‑else and loops matter in Java

Imagine you’re playing a video game where you must choose a path based on a clue, or repeat a task until you win. That’s exactly what if‑else (a decision) and loops (repetition) let your Java program do.

💡 In Simple Words: An if‑else tells the computer, "If this is true, do X; otherwise, do Y." A loop says, "Keep doing this as long as a condition stays true."

If‑else Statements in Java

What is an if‑else?

An if‑else is a control structure that lets you run one block of code when a condition is true and another block when it’s false. Think of it like a traffic light: green means go (run the first block), red means stop and take a detour (run the else block).

Basic syntax

if (condition) {
    // code when condition is true
} else {
    // code when condition is false
}

condition is any expression that evaluates to true or false (like score >= 40).

Worked example

Suppose you need to print whether a student passed:

int marks = 68;
if (marks >= 40) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Because 68 >= 40 is true, the output is Pass.

Nested if‑else

You can place an if‑else inside another if‑else to handle more than two cases, like grading:

int marks = 76;
if (marks >= 80) {
    grade = "A";
} else if (marks >= 60) {
    grade = "B";
} else if (marks >= 40) {
    grade = "C";
} else {
    grade = "F";
}

This chain checks each condition in order until one matches.

Loops in Java

Why use loops?

Imagine washing a pile of dishes by hand—tedious, right? A loop is like a dishwasher that repeats the same action automatically until the job is done.

Types of loops

  • for loop: Best when you know exactly how many times you need to repeat.
  • while loop: Runs while a condition stays true; the condition is checked before each iteration.
  • do‑while loop: Similar to while, but the block runs at least once because the condition is checked after the iteration.

for loop

Syntax:

for (initialization; condition; update) {
    // code to repeat
}

Example: Print numbers 1 to 5.

for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}

while loop

Syntax:

while (condition) {
    // code to repeat
}

Example: Keep asking for a password until it matches.

String correct = "java123";
String input = "";
while (!input.equals(correct)) {
    input = scanner.nextLine();
    System.out.println("Try again!");
}
System.out.println("Access granted");

do‑while loop

Syntax:

do {
    // code runs first
} while (condition);

Example: Show a menu at least once.

int choice;
do {
    System.out.println("1. Add\n2. Exit");
    choice = scanner.nextInt();
} while (choice != 2);

Comparison table

LoopWhen to useCondition checkGuaranteed first run?
forKnown number of iterationsBefore each iterationNo
whileRepeating until a condition changesBefore each iterationNo
do‑whileNeed at least one executionAfter each iterationYes

Flow of an if‑else decision

graph TD\nA[Start] --> B{Condition?} --> C[True block] --> D[End]\nB --> E[False block] --> D

Common pitfalls to avoid

  • Forgetting braces {} can cause only the next statement to be part of the if or loop.
  • Using = (assignment) instead of == (equality) in a condition.
  • Creating an infinite loop by never updating the condition variable.

📝 Likely Exam Questions

  1. Write a Java program using an if‑else statement to display "Even" or "Odd" for a given integer.
    int n = 7;
    if (n % 2 == 0) {
    System.out.println("Even");
    } else {
    System.out.println("Odd");
    }
  2. Explain the difference between a while loop and a do‑while loop with an example.
    Answer: A while loop checks the condition before executing the block, so it may never run. A do‑while loop checks after the block, guaranteeing at least one execution. Example code shown above.
  3. What will be the output of the following code?
    for (int i = 1; i < 4; i++) {
    if (i == 2) continue;
    System.out.print(i + " ");
    }
    Answer: 1 3 (the continue skips printing when i is 2).
  4. Convert the while loop below into an equivalent for loop.
    int i = 0;
    while (i < 5) {
    System.out.println(i);
    i++;
    }
    Answer: for (int i = 0; i < 5; i++) { System.out.println(i); }
  5. Describe a scenario where a nested if‑else is preferable over multiple separate if statements.
    Answer: When you need to choose one grade from several mutually exclusive ranges, a nested if‑else ensures only one block runs.
#ICSE#Class 10#Java#if-else#loops