A conditional statement allows a Java program to make a decision based on a condition.
The fundamental idea is:
Condition
↓
┌───────┴───────┐
↓ ↓
true false
↓ ↓
execute path alternative path
Java's main conditional constructs are:
1. if
2. if-else
3. else-if ladder
4. nested if
5. switch
We'll also understand conditions, boolean expressions, braces, break, fall-through, nested decisions, and when to use each one.
A condition is an expression whose result is either:
true
or
false
Example:
int age = 20;
age >= 18The result is:
20 >= 18
↓
true
Therefore:
if (age >= 18) {
System.out.println("Adult");
}Output:
Adult
Java provides comparison operators:
| Operator | Meaning | Example |
|---|---|---|
> |
greater than | a > b |
< |
less than | a < b |
>= |
greater than or equal | a >= b |
<= |
less than or equal | a <= b |
== |
equal to | a == b |
!= |
not equal to | a != b |
Example:
int a = 10;
int b = 20;
System.out.println(a < b);Output:
true
This is one of the most important beginner doubts.
Assignment:
int x = 10;Means:
Put
10intox.
Comparison:
x == 10Means:
Is
xequal to10?
So:
if (x == 10) {
System.out.println("Yes");
}Correct.
The if statement executes a block of code only when its condition is true.
if (condition) {
// statements
}int age = 20;
if (age >= 18) {
System.out.println("Eligible");
}Output:
Eligible
int age = 15;
if (age >= 18) {
System.out.println("Eligible");
}The condition:
15 >= 18
is:
false
Therefore the body is skipped.
Output:
(no output)
This is the defining characteristic of if.
Start
↓
Condition
↙ ↘
true false
↓ ↓
if body Skip
↓ ↓
└──────→ Continue
Sometimes you don't want to do nothing when the condition is false.
You want an alternative.
That's where else comes in.
if (condition) {
// true block
}
else {
// false block
}int number = 10;
if (number % 2 == 0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}Output:
Even
Condition
↙ ↘
true false
↓ ↓
if block else block
↓ ↓
└─────┬─────┘
↓
Exit
Exactly one of the two branches executes.
if block → executes if true
else block → executes if false
❌ No.
This is invalid:
else {
System.out.println("Hello");
}else must be associated with an if.
✅ Yes.
if (age >= 18) {
System.out.println("Adult");
}else is optional.
Suppose you have several possible conditions.
Example:
90+ → A
75+ → B
60+ → C
below 60 → Fail
You can use an else-if ladder.
int marks = 82;
if (marks >= 90) {
System.out.println("A");
}
else if (marks >= 75) {
System.out.println("B");
}
else if (marks >= 60) {
System.out.println("C");
}
else {
System.out.println("Fail");
}Output:
B
Java checks from top to bottom.
For:
int marks = 82;Java asks:
marks >= 90 ?
No.
Then:
marks >= 75 ?
Yes.
So:
Execute B
↓
STOP CHECKING REMAINING CONDITIONS
It does not continue checking the 60 condition after finding the matching branch.
Look at this:
int marks = 95;
if (marks >= 60) {
System.out.println("C");
}
else if (marks >= 90) {
System.out.println("A");
}Output:
C
Why?
Because:
95 >= 60
is already true.
Java enters the first branch and never reaches the else-if.
Therefore:
In an
else-ifladder, the order of conditions matters.
Usually, when checking ranges like marks, put the most restrictive/highest threshold first.
This is different:
if (marks >= 60) {
System.out.println("C");
}
if (marks >= 90) {
System.out.println("A");
}For:
marks = 95
both conditions are true.
Output:
C
A
Why?
Because these are two separate if statements.
Compare:
if
else-if
else
with:
if
if
They are not the same.
if (condition1) {
}
if (condition2) {
}
if (condition3) {
}Potentially multiple blocks can execute.
if (condition1) {
}
else if (condition2) {
}
else if (condition3) {
}Only the first matching branch executes.
Separate
ifs = independent decisions.
else-if= one connected decision chain.
A conditional statement inside another conditional statement is called a nested if.
Example:
int age = 25;
boolean citizen = true;
if (age >= 18) {
if (citizen) {
System.out.println("Eligible");
}
}Execution:
age >= 18?
↓
true
↓
citizen?
↓
true
↓
Eligible
Suppose the second condition only makes sense if the first condition is true.
For example:
First:
Is the person an adult?
Then:
Is the person a citizen?
Only if both are relevant do we check the second condition.
That's a natural nested decision.
This:
if (age >= 18) {
if (citizen) {
System.out.println("Eligible");
}
}can often be expressed as:
if (age >= 18 && citizen) {
System.out.println("Eligible");
}Both can represent the same logical requirement in simple cases.
The second is often more concise.
Java provides:
&& AND
|| OR
! NOT
Both conditions must be true.
if (age >= 18 && citizen) {
System.out.println("Eligible");
}Truth table:
| A | B | A && B |
|---|---|---|
| false | false | false |
| false | true | false |
| true | false | false |
| true | true | true |
Memory:
AND = everyone must agree.
At least one condition must be true.
if (day == 6 || day == 7) {
System.out.println("Weekend");
}Truth table:
| A | B | A || B | | ----- | ----- | -------- | | false | false | false | | false | true | true | | true | false | true | | true | true | true |
Memory:
OR = at least one is enough.
Reverses a boolean result.
boolean raining = false;
if (!raining) {
System.out.println("Go outside");
}Since:
raining = false
then:
!raining = true
This is a deeper but important concept.
With:
A && Bif A is already false, Java doesn't need to evaluate B to know the whole expression is false.
Similarly:
A || Bif A is already true, Java doesn't need to evaluate B.
Example:
if (x != 0 && 10 / x > 2) {
System.out.println("Valid");
}If x == 0:
x != 0 → false
Java stops there, so:
10 / x
is not evaluated.
This prevents division by zero in this example.
Now suppose you have one expression and several fixed possible values.
For example:
1 → Monday
2 → Tuesday
3 → Wednesday
A switch can make this cleaner.
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid");
}Output:
Tuesday
Given:
int day = 2;Java evaluates:
switch expression
↓
2
↓
compare with cases
↓
case 1? No
↓
case 2? YES
↓
execute case 2
↓
break
↓
exit switch
A case represents one possible matching value.
case 1:means:
If the switch expression matches
1, execute this section.
default is the fallback branch.
Example:
int day = 8;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}Since no case matches:
default
executes.
default is optional.
Consider:
int x = 1;
switch (x) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
}Output:
One
Two
Three
Why?
Because there is no break.
Java enters case 1 and then continues executing subsequent statements.
This behavior is called:
fall-through
switch (x) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
}Now:
case 1
↓
print One
↓
break
↓
exit switch
❌ Not necessarily.
There are valid situations where intentional fall-through is useful.
But as a beginner, remember:
If you don't want execution to continue into the next case, use
break.
You can intentionally group cases.
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}For 6:
case 6
↓
case 7
↓
Weekend
↓
break
This is intentional fall-through to share the same action.
This is a common exam question.
Better when conditions involve:
ranges
complex expressions
multiple variables
logical operators
Example:
if (marks >= 90)or:
if (age >= 18 && citizen)Useful when one expression is being matched against specific alternatives.
Example:
switch (day) {
case 1:
case 2:
}A traditional case does not work like:
case marks >= 90:That is not the normal switch case syntax.
For ranges such as:
90–100
75–89
60–74
an if-else ladder is generally the natural choice:
if (marks >= 90) {
...
}
else if (marks >= 75) {
...
}Yes.
Example:
String day = "Monday";
switch (day) {
case "Monday":
System.out.println("Start");
break;
case "Sunday":
System.out.println("Holiday");
break;
default:
System.out.println("Other");
}Yes.
char grade = 'A';
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
default:
System.out.println("Other");
}Traditional switch does not use boolean as a switch selector.
For boolean conditions, use:
ifFor example:
if (isLoggedIn) {
System.out.println("Welcome");
}In Java:
if (10 > 5) {
System.out.println("Yes");
}is valid.
But:
if (10) {
System.out.println("Yes");
}is not valid Java.
Unlike some languages, Java does not treat an integer like 1 as true.
The condition must evaluate to a boolean.
This is invalid Java:
int x = 10;
if (x) {
}Correct:
if (x != 0) {
}because:
x != 0
produces a boolean.
Absolutely.
boolean eligible = true;
if (eligible) {
System.out.println("Eligible");
}This is perfectly valid.
Consider:
if (a > 0)
if (b > 0)
System.out.println("Both positive");
else
System.out.println("What?");Which if does the else belong to?
By Java's rule:
An
elseis associated with the nearest unmatchedif.
So it belongs to:
if (b > 0)not the outer if.
Instead of:
if (a > 0)
if (b > 0)
...
else
...write:
if (a > 0) {
if (b > 0) {
System.out.println("Both positive");
}
else {
System.out.println("B is not positive");
}
}Braces make the structure obvious.
Yes.
switch (day) {
case 1:
if (holiday) {
System.out.println("Holiday");
}
break;
}Likewise, a switch can appear inside an if.
Conditional constructs can be nested.
Don't confuse:
if
with the:
?:
operator.
Example:
int max = (a > b) ? a : b;This is the ternary conditional operator, not an if statement.
It is useful for compact expressions.
Let's combine several concepts.
class Student {
public static void main(String[] args) {
int marks = 82;
if (marks < 0 || marks > 100) {
System.out.println("Invalid marks");
}
else if (marks >= 90) {
System.out.println("A");
}
else if (marks >= 75) {
System.out.println("B");
}
else if (marks >= 60) {
System.out.println("C");
}
else {
System.out.println("Fail");
}
}
}For:
marks = 82
execution is:
Invalid? → No
90+? → No
75+? → Yes
↓
B
When solving a conditional problem, ask:
What decision?
↓
┌─────────┴─────────┐
↓ ↓
One condition Multiple choices
↓ ↓
if What type?
↙ ↘
Conditions Fixed values
↓ ↓
if/else switch
│
Multiple ranges?
↓
else-if
| Construct | Meaning | Example Situation |
|---|---|---|
if |
Execute if true | age >= 18 |
if-else |
Choose one of two paths | Even/odd |
else-if |
Choose among conditions | Grade calculation |
Nested if |
Decision inside decision | Eligibility checks |
switch |
Match fixed alternatives | Menu/day/command |
✅ Yes.
❌ No.
✅ Yes.
❌ No. Only the first matching branch executes.
It checks sequentially until it finds the first true condition.
❌ No.
if (...)
else if (...)is valid.
❌ No.
❌ No, but without it, execution can fall through into later cases.
❌ No.
if (10) // invalidUse a boolean expression:
if (10 > 5)❌ Traditional case matching is for specific case values, not ordinary range conditions. Use if-else for ranges.
CONDITIONALS
│
┌──────────────┼──────────────┐
↓ ↓ ↓
if if-else else-if
│ │ │
One decision Two paths Many conditions
│
↓
Nested if
│
Decision inside
decision
+
switch
│
Fixed choices
│
case → break
1.
if→ execute when condition is true.
2.
if-else→ choose between two paths.
3.
else-if→ check multiple conditions from top to bottom; first true branch wins.
4. Nested
if→ put one decision inside another.
5.
switch→ match one expression against fixed alternatives;breakprevents unwanted fall-through.