Java If … Else

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 13 / 50 · English

Java If … Else

Choose the path that matches the condition.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind If … Else.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

An if/else chain checks conditions in order. The first true branch runs, and the rest of that chain is skipped. Put more specific or stricter thresholds before broader ones when the conditions overlap.

02 / Step by step

Explore the details

Java Conditions and If Statements

Conditions let your program make decisions — running different code depending on whether something is true or false.

Think of it like real life: "If it rains, take an umbrella. Otherwise, wear sunglasses." Your program does the same — it checks a condition, then takes the right path.

The if Statement

Use if to run a block of code only when a condition is true. If the condition is false, the block is skipped entirely.

Code anatomy / கோடின் விளக்கம்

CodeMeaning
ifKeyword that starts a conditional check
(Opening parenthesis — condition goes inside
ageThe variable being checked
>= Comparison operator: greater than or equal to
18The value to compare against
)Closing parenthesis
{Opening brace — code block starts here
}Closing brace — code block ends here

The else Statement

Add an else block to handle the case when the condition is false. Only one of the two blocks will ever run — never both.

The else if Statement

When you need to check more than two possibilities, chain else if blocks. Java checks each condition from top to bottom and runs the first one that is true.

Conditions Use Comparison Operators

Every if condition evaluates to true or false. You build conditions using comparison operators:

  • a == b — Equal to
  • a != b — Not equal to
  • a > b — Greater than
  • a < b — Less than
  • a >= b — Greater than or equal to
  • a <= b — Less than or equal to

Always use curly braces { } even for single-line if blocks. Without them, only the very next line belongs to the if — a common source of bugs.

Java reference fragmentJAVA
int score = 75;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else if (score >= 70) {
    System.out.println("C");
} else {
    System.out.println("Below C");
}
// Output: C

// Ternary shorthand
String result = (score >= 50) ? "Pass" : "Fail";
System.out.println(result);  // Pass
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        int score = 75;
        if (score >= 90) {
            System.out.println("Excellent");
        } else if (score >= 60) {
            System.out.println("Pass");
        } else {
            System.out.println("Try again");
        }
    }
}
Predict first. Then reveal the expected output / வெளியீடு
Pass

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Test 59, 60, 89, and 90. Which branch handles each boundary?

Reveal the answer and reasoning / விடை

59 → Try again; 60 → Pass; 89 → Pass; 90 → Excellent.

One mistake to avoid: Writing score >= 60 before score >= 90 would make the later excellent branch unreachable for these values.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.