Java If … Else
Choose the path that matches the condition.
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.
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.
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 / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
if | Keyword that starts a conditional check |
( | Opening parenthesis — condition goes inside |
age | The variable being checked |
>= | Comparison operator: greater than or equal to |
18 | The 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 toa != b— Not equal toa > b— Greater thana < b— Less thana >= b— Greater than or equal toa <= 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.
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); // PassA 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.
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.
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.
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.
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.