Java If-Else Statement
The if-else statement is one of the most fundamental decision-making structures in programming. It allows your program to execute different code based on whether a condition is true or false. In Java, we use the if-else statement to control the flow of execution depending on certain conditions.
In this blog, we'll explore how the if-else statement works in Java, provide explanations, and demonstrate examples with expected outputs.
What is an If-Else Statement?#
An if-else statement is used to execute one block of code if a condition is true, and another block if the condition is false. It's a way to introduce logic in your program, so it can make decisions.
The syntax for an if-else statement in Java is:
Flow of Execution#

- If condition: The condition inside the
ifis evaluated. If the condition istrue, the block of code inside theifis executed. - Else block: If the condition is
false, the block of code inside theelseis executed.
Basic Example: If-Else Statement#
Let's start with a simple example to see how the if-else statement works.
Example 1: Basic If-Else Statement#
Explanation:
- The condition
number > 0is evaluated. Since the value ofnumberis 10, which is greater than 0, the condition istrue, and the code inside theifblock is executed. - If the number was negative or zero, the code inside the
elseblock would have been executed.
Expected Output:
If-Else with Multiple Conditions: Using else if#
Sometimes, we want to check for more than two conditions. In this case, we can use the else if statement to check additional conditions after the initial if condition.
Example 2: If-Else-If Statement#
Explanation:
- The first condition checks if the number is positive. Since the number is 0, it moves to the
else ifcondition and checks if the number is negative. - Finally, since the number is neither positive nor negative, the
elseblock is executed, and the program prints "The number is zero."
Expected Output:
Nested If-Else Statement#
A nested if-else statement is when you place an if-else statement inside another if or else block. This is useful when you have more complex conditions to check.
Example 3: Nested If-Else Statement#
Explanation:
- The first
ifchecks if the age is greater than or equal to 18. - Inside the
ifblock, there's anotherifthat checks if the age is exactly 18. - If the age is 18, it prints a specific message for first-time voters. Otherwise, it simply prints that the person is eligible to vote.
Expected Output:
If-Else with Logical Operators#
You can also combine multiple conditions in an if statement using logical operators like && (AND), || (OR), and ! (NOT). This helps you to create more complex conditions.
Example 4: If-Else with Logical AND (&&)#
Explanation:
- The condition
age >= 18 && hasVoterIDchecks if both conditions are true. Since both are true in this case (age is 25 and the person has a voter ID), theifblock is executed.
Expected Output:
Example 5: If-Else with Logical OR (||)#
Explanation:
- The condition
age >= 18 || hasParentalConsentchecks if either the age is 18 or older or the person has parental consent. - Since the person is 17 but has parental consent, the
ifblock is executed.
Expected Output:
Conclusion#
In this blog, we've learned:
- The if-else statement allows you to make decisions in your program by checking conditions.
- The if-else structure can handle simple or multiple conditions using
if,else if, andelse. - We can also use logical operators like
&&(AND) and||(OR) to combine multiple conditions. - Nested if-else statements allow more complex logic.
Mastering the if-else statement is crucial for writing programs that make decisions based on data, and it’s a fundamental concept in programming. With these building blocks, you can create powerful decision-making logic in your Java applications.