In programming, we often need to make decisions based on a condition. For example, if a student's marks are 40 or above, we want to say that the student passed. If the marks are below 40, we want to say that the student failed.
This is where conditional statements come into the picture. They allow our program to make decisions and execute different pieces of code depending on whether a condition is true or false.
Java provides several ways to write conditions, such as if, if-else, else-if, and switch.
The if Statement
The simplest conditional statement is if.
We use it when we want to execute some code only when a condition is true.
if (condition) {
// code to execute
}
For example:
int age = 20;
if (age >= 18) {
System.out.println("You can vote.");
}
Here, Java checks whether age >= 18 is true. Since age is 20, the condition is true, so the message is printed.
If the condition were false, Java would simply skip the code inside the if block.
The if-else Statement
What if we want to do one thing when the condition is true and something else when it is false? That's where if-else is useful.
int age = 16;
if (age >= 18) {
System.out.println("You can vote.");
} else {
System.out.println("You cannot vote yet.");
}
Output:
You cannot vote yet.
Since 16 >= 18 is false, Java skips the if block and executes the else block.
So you can think of if-else like this:
If this condition is true, do this. Otherwise, do that.
The else-if Statement
Sometimes we have more than two possibilities. For example, suppose we want to assign a grade based on marks.
We can use else-if:
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A+");
} else if (marks >= 80) {
System.out.println("Grade A");
} else if (marks >= 70) {
System.out.println("Grade B");
} else if (marks >= 40) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
Output:
Grade A
Java checks the conditions from top to bottom. As soon as it finds a condition that is true, it executes that block and skips the remaining conditions.
This is useful when there are several possible outcomes.
Nested if
We can also put one if statement inside another if statement. This is called a nested if.
For example:
int age = 25;
boolean hasTicket = true;
if (age >= 18) {
if (hasTicket) {
System.out.println("You can enter.");
}
}
Here, the second condition is checked only if the first condition is true.
Nested conditions can be useful, but don't overuse them. If the logic becomes too deeply nested, the code can become difficult to read.
The switch Statement
When we need to compare one value against several specific options, Java also provides the switch statement.
For example:
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 day");
}
Output:
Tuesday
Java checks the value of day and finds the matching case.
The break statement tells Java to stop the switch after the matching case has been executed.
The default block runs when none of the cases match.
For simple conditions and ranges, if-else is often more convenient. switch is particularly useful when you have several specific choices to compare.
Why Are Conditional Statements Important?
Without conditional statements, a program would simply execute the same instructions every time.
Imagine a login application. It needs to check whether the entered password is correct. A shopping application needs to check whether an item is in stock. A game needs to check whether the player has won or lost.
All of these situations require the program to make decisions.
That's exactly what conditional statements allow us to do.
Condition
↓
Is it true?
↙ ↘
Yes No
↓ ↓
Do A Do B
Once you understand conditional statements, your programs become much more useful because they can react differently to different situations.