if-else Statement in Java
In this lesson: Decision making in Java with if, if-else, else-if ladder and nested if — programs with output. In Hindi & English.
Making decisions
if-else statements let your program make decisions — do one thing if a condition is true, another if it is false. This is how programs react differently to different inputs, like showing "Pass" or "Fail" based on marks.
The if statement
int marks = 75;
if (marks >= 40) {
System.out.println("Pass");
}
The if block runs only when the condition in brackets is true. If marks were below 40, nothing would print here.
if-else
int marks = 30;
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
With else, one of the two blocks always runs. If the condition is true, the if block runs; otherwise the else block does.
The else-if ladder
int marks = 82;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 40) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
An else-if ladder checks conditions in order and runs the first true one. Here 82 is not >= 90 but is >= 75, so it prints Grade B and skips the rest.
Nested if
int age = 20;
boolean hasId = true;
if (age >= 18) {
if (hasId) {
System.out.println("Allowed");
}
}
An if can contain another if. Both conditions must be true here. For simple cases, you could also combine them with &&.
The ternary operator
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max);
The ternary condition ? x : y is a compact if-else that returns a value. If the condition is true it gives x, else y — handy for short choices.
Common mistakes
- Using
=instead of==. Conditions compare with==, not assign with=. - Missing braces. Without
{ }, only the next line belongs to the if. - Wrong ladder order. Put stricter conditions first, or a looser one catches everything.
- Semicolon after if.
if (x);makes the if do nothing — do not add;.
Summary
ifruns a block when a condition is true;elsewhen false.- An else-if ladder picks the first true condition among many.
- Nested if checks a condition inside another.
- The ternary
cond ? x : yis a one-line if-else that returns a value. - Use
==to compare and always prefer curly braces.
Next, handle many cases with switch-case, or repeat actions with Java loops.
Frequently Asked Questions
What is an if-else statement in Java?
if block runs when a condition is true, and the else block runs when it is false. It is how programs choose between two paths.What is an else-if ladder?
if ... else if ... else if ... else. Java checks each in order and runs the first one that is true. It is used when there are more than two possible outcomes, like grading.What is the ternary operator in Java?
condition ? valueIfTrue : valueIfFalse. For example int max = a > b ? a : b;. It is handy for simple choices in one line.Why must if conditions use == and not =?
= assigns a value, while == compares. An if needs a true/false test, so it uses ==. Using = in a condition is a common bug (and often a compile error in Java).Do I always need curly braces with if?
{ } is safer and clearer. Without braces, only the next single line belongs to the if, which can cause subtle bugs.