The if-else
statement in Java is a powerful decision-making tool used to control the program’s flow based on conditions. It executes one block of code if a condition is true
and another block if the condition is false
. In this article, we will learn Java if-else statement
with examples.
Example:
Java
// Java Program to demonstrate
// if-else statement
public class IfElse {
public static void main(String[] args) {
int n = 10;
if (n > 5) {
System.out.println("The number is greater than 5.");
} else {
System.out.println("The number is 5 or less.");
}
}
}
OutputThe number is greater than 5.
Dry-Running the Above Example:
- Program starts.
n
is initialized to 10
.if
condition is checked: 10 > 5
, yields true
.- Block inside the
if
statement executes: "The number is greater than 5."
is printed.
- The
else
block is skipped as the condition is true
.
Syntax of if-else Statement
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Working of if-else Statement
- Control falls into the
if
block. - The flow jumps to the condition.
- The condition is tested:
- If the condition yields
true
, go to Step 4. - If the condition yields
false
, go to Step 5.
- The
if
block or the body inside the if
is executed. - If the condition is
false
, the else
block is executed instead. - Control exits the
if-else
block.
Flowchart of Java if-else Statement
Below is the Java if-else flowchart.
In the above flowchart of Java if-else, it states that the condition is evaluated, and if it is true, the if
block executes; otherwise, the else
block executes, followed by the continuation of the program.
Nested if statement in Java
In Java, we can use nested if statements to create more complex conditional logic. Nested if statements are if statements inside other if statements.
Syntax of Nested if
if (condition1) {
// code block 1
if (condition2) {
// code block 2
}
}
Example:
Java
// Java Program to implement
// Nested if statement
public class NestedIf {
public static void main(String[] args) {
int a = 25;
double w = 65.5;
if (a >= 18) {
if (w >= 50.0) {
System.out.println("You are eligible to donate blood.");
} else {
System.out.println("You must weigh at least 50 kilograms to donate blood.");
}
} else {
System.out.println("You must be at least 18 years old to donate blood.");
}
}
}
OutputYou are eligible to donate blood.
Note: The first print statement is in a block of “if” so the second statement is not in the block of “if”. The third print statement is in else but that else doesn’t have any corresponding “if”. That means an “else” statement cannot exist without an “if” statement.
Java if-else Statement – FAQs
What is the if-else Statement in Java?
The if-else statement in Java allows you to execute one block of code when a condition is true and another block when the condition is false.
What is the Syntax for if-else Statement?
if (condition) {
// Executes this block if
// condition is true
}
else {
// Executes this block if
// condition is false
}
Can an else Block Exist without an if Statement?
No, an else block must always be associated with an if statement. It cannot exist independently.
What Happens if the if Condition is false and there is no else Block?
If the if condition evaluates to false and there is no else block, the program skips the if block and continues execution with the next statement.
Can We Use Multiple if-else Statements in a Program?
Yes, we can use multiple if-else statements in a program, and they can also be nested to handle complex decision-making.