Let's learn this from zero → understanding → writing programs.
The main goal is not to memorize syntax. You should be able to look at a problem and decide which conditional statement to use.
Imagine your Java program is asking:
"Is the student eligible?"
The program needs to check a condition:
age >= 18If the answer is true:
Eligible
If the answer is false:
Not Eligible
That's conditional programming.
CONDITION
↓
┌────────┴────────┐
↓ ↓
TRUE FALSE
↓ ↓
Do this Do that
A Java condition produces a boolean value:
true
or
false
For example:
10 > 5produces:
true
And:
10 < 5produces:
false
Let's start with the simplest conditional statement.
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}Read this as English:
IF age is greater than or equal to 18, print "Adult".
Since:
20 >= 18
is true, output is:
Adult
Change:
int age = 15;Now:
if (age >= 18) {
System.out.println("Adult");
}Java checks:
15 >= 18
↓
false
So Java simply skips the block.
No output
ifhas no alternative instruction.
If false → do nothing and continue.
Suppose you don't want Java to do nothing.
You want:
18 or above → Adult
below 18 → Minor
Use if-else.
int age = 15;
if (age >= 18) {
System.out.println("Adult");
}
else {
System.out.println("Minor");
}Output:
Minor
Imagine one door with two paths:
age >= 18?
/ \
YES NO
↓ ↓
Adult Minor
Only one path can be taken.
That's if-else.
Suppose:
90+ → A
75+ → B
60+ → C
below 60 → Fail
Can we use just if-else?
Not conveniently, because there are more than two possibilities.
We 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
This is extremely important.
Java checks from top to bottom.
For:
marks = 82Java asks:
82 >= 90?
❌ No.
Then:
82 >= 75?
✅ Yes.
So Java prints:
B
Then it stops checking the remaining branches.
The first true condition wins.
Suppose:
int x = 100;
if (x >= 50) {
System.out.println("A");
}
else if (x >= 80) {
System.out.println("B");
}What prints?
A
Why?
Because:
100 >= 50
is already true.
Java never reaches:
else if (x >= 80)Order matters.
This causes huge confusion.
if (x >= 50) {
System.out.println("A");
}
if (x >= 80) {
System.out.println("B");
}If:
x = 100
Output:
A
B
Both conditions are checked independently.
if (x >= 50) {
System.out.println("A");
}
else if (x >= 80) {
System.out.println("B");
}Output:
A
Only the first matching branch executes.
if + if + if
↓
Independent decisions
if + else-if + else
↓
One decision chain
Now imagine:
First check whether the person is 18+. If yes, check whether they are a citizen.
That's a decision inside another decision.
int age = 20;
boolean citizen = true;
if (age >= 18) {
if (citizen) {
System.out.println("Eligible");
}
}This is called nested if.
age >= 18?
↓
YES
↓
citizen?
/ \
YES NO
↓ ↓
Eligible ...
So:
Nested = one conditional statement inside another conditional statement.
Sometimes yes.
Instead of:
if (age >= 18) {
if (citizen) {
System.out.println("Eligible");
}
}we can write:
if (age >= 18 && citizen) {
System.out.println("Eligible");
}Here && means AND.
Both conditions must be true.
Suppose:
boolean student = true;
boolean paidFees = true;Then:
if (student && paidFees) {
System.out.println("Allowed");
}Read it as:
If the person is a student AND has paid the fees.
Both must be true.
true && true → true
true && false → false
false && true → false
false && false → false
&&= AND = both required
Suppose:
Saturday OR Sunday
means either one is enough.
if (day == 6 || day == 7) {
System.out.println("Weekend");
}|| means OR.
true || true → true
true || false → true
false || true → true
false || false → false
||= OR = at least one
! means NOT.
Suppose:
boolean raining = false;Then:
!rainingmeans:
NOT false
↓
true
Example:
if (!raining) {
System.out.println("Go outside");
}!true → false
!false → true
Suppose you have:
1 → Monday
2 → Tuesday
3 → Wednesday
4 → Thursday
You could write:
if (day == 1) {
...
}
else if (day == 2) {
...
}
else if (day == 3) {
...
}But this is a good situation for switch.
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
Think:
day = 2
switch
↓
┌────┼────┐
↓ ↓ ↓
1 2 3
↓ ↓ ↓
Monday Tuesday Wednesday
↑
MATCH
Java finds:
case 2
and executes it.
This is extremely important in switch.
case 2:
System.out.println("Tuesday");
break;break tells Java:
"I found my case. Leave the switch now."
Without break, Java can continue into the next cases.
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.
This is called:
Fall-through
Java entered case 1 and continued executing subsequent statements.
Yes.
Suppose:
Saturday → Weekend
Sunday → Weekend
You can intentionally group them:
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}Output:
Weekend
Here case 6 deliberately falls through to the shared code.
default means:
"None of the cases matched."
Example:
int day = 10;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}Output:
Invalid day
default is similar to the else concept.
Conceptually:
if-else
has:
if → matching condition
else → nothing matched
switch has:
case → matching value
default → nothing matched
They're not technically identical, but this comparison is useful for understanding.
Use if when you have one condition.
Example:
if (temperature > 40) {
System.out.println("Very hot");
}Use it when there are two alternatives.
Example:
if (number % 2 == 0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}Think:
YES / NO
TRUE / FALSE
Use it when you have multiple conditions or ranges.
Example:
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");
}Use nested if when:
One decision depends on another decision.
Example:
Is account active?
↓ YES
Is password correct?
↓ YES
Login successful
Use switch when you have one value and several fixed choices.
Examples:
menu choice
day number
month number
command
grade character
option selection
Example:
switch (choice) {
case 1:
...
break;
case 2:
...
break;
}Use this simple rule:
Is it a range/complex condition?
↓
YES
↓
if-else
Example:
if (marks >= 75)But:
Is it one value matching fixed choices?
↓
YES
↓
switch
Example:
switch (day)Let's build a small student grading program.
class Student {
public static void main(String[] args) {
int marks = 85;
if (marks < 0 || marks > 100) {
System.out.println("Invalid marks");
}
else if (marks >= 90) {
System.out.println("Grade A");
}
else if (marks >= 75) {
System.out.println("Grade B");
}
else if (marks >= 60) {
System.out.println("Grade C");
}
else {
System.out.println("Fail");
}
}
}Trace it:
marks = 85
85 < 0 || 85 > 100
↓
false
85 >= 90
↓
false
85 >= 75
↓
true
Grade B
Output:
Grade B
int x = 10;
if (x > 5) {
System.out.println("A");
}What is printed?
Answer
A
Because:
10 > 5 → true
int x = 10;
if (x > 20) {
System.out.println("A");
}
else {
System.out.println("B");
}Answer:
B
Because:
10 > 20 → false
int x = 80;
if (x >= 50) {
System.out.println("A");
}
else if (x >= 75) {
System.out.println("B");
}Answer:
A
Why?
Because the first condition is already true.
int x = 80;
if (x >= 50) {
System.out.println("A");
}
if (x >= 75) {
System.out.println("B");
}Answer:
A
B
Because these are independent if statements.
Don't memorize five unrelated syntax structures.
Think of them as different ways of asking questions.
"Is this true?"
if (condition)"Is this true? If not, do the other thing."
if (condition)
else"Which condition is true?"
if (...)
else if (...)
else if (...)
else"If this is true, ask another question."
if (...) {
if (...) {
}
}"Which fixed value did I receive?"
switch (value)CONDITIONAL STATEMENTS
│
├── if
│ └── one condition
│
├── if-else
│ └── two paths
│
├── else-if ladder
│ └── multiple conditions
│
├── nested if
│ └── decision inside decision
│
└── switch
└── fixed choices
And remember:
&& → AND → both
|| → OR → at least one
! → NOT → reverse
if false
↓
skip
if-else
↓
one of two
else-if
↓
first true wins
switch
↓
matching case
break
↓
leave switch
Use
iffor one decision,if-elsefor two paths,else-iffor multiple conditions, nestediffor dependent decisions, andswitchfor multiple fixed choices.