Introduction
Java Fundamentals
Java Flow Control
Java Arrays
Object Oriented Programming
If-Else Statement in Java With Examples
In this tutorial, we will get a basic overview of control flow statements using Java if, if-else, if-else-if, and Nested if-else statement with examples.
1. If Statement in Java:
If Statement in Java is used to test the condition. This is a powerful decision-making statement. It is used to control the flow of execution of the statement. The 'if' keyword is followed by a test expression. It can be any logical expression. It is followed by a statement or a block of statement to be executed if the test expression evaluates to true.
Flow Chart for if statement in Java
Java If Statement Example:
//Example 1 :
if (balance < 1000) {
System.out.println("Low Balance");
}
//Example 2 :
if (debitAmt < bal) {
System.out.println("Cannot withdraw Amount");
}
- In Example 1, the 'if' statement checks whether the balance is less than 1000. If the condition evaluates to true, then "Low Balance" is printed.
- In Example 2, the 'if' statement checks whether the amount to be debited from an account is less than the balance. If the condition evaluates to true, then the message "Cannot withdraw Amount" is printed.
2. If-Else Statement in Java:
- The 'if-else' statement provides a way to take action in both cases - whether the test expression evaluates to true or false.
- If the condition in the expression of the 'if' statement evaluates to false, then the statement(s) in the else block is executed.
Java If-Else Statement Example
//Example 1 :
if (balance < 1000) {
System.out.println("Low Balance");
} else {
System.out.println("Balance Ok");
}
- Example 1, demonstrates how the 'if-else' statement can be used to check whether the balance is low or not.
- If the condition in the expression (balance < 1000) evaluates to true, it means that the balance is low. So the message "Low Balance" is printed.
- If the expression to false, that is, the balance is greater than 1000, then the balance is as required and the message "Balance Ok" is printed.
3. If-Else-If Statement in Java:
The 'if-else-if statement' is also called as if-else-if ladder statement.It is use to check multiple conditions.
if (number > 0) {
System.out.println("Positive number");
} else if (number < 0) {
System.out.println("Negative number");
} else {
System.out.println("Zero");
}
- Example 1, demonstrates how the 'if-else' statement can be used to check whether the balance is low or not.
- If the condition in the expression (balance < 1000) evaluates to true, it means that the balance is low. So the message "Low Balance" is printed.
- If the expression to false, that is, the balance is greater than 1000, then the balance is as required and the message "Balance Ok" is printed
4. Nested If-Else Statement in Java:
Used when more than one 'if-else' statement is in nested form. Sometimes, it may be required to check another condition after the first one has been evaluated as true. In such a situation, the nested 'if-else' statement is used. One 'if-else' statement can occur inside another 'if-else' statement. The inner 'if-else' statement is said to be nested in the outer 'if-else' statement. There can be more than one nested if-else statement and the nesting can go up to any level.
Nested If-Else Statement Example in Java:
if (age >= 60) { //condition 1
if (gender == 'f') //condtion 2 {
System.out.println("9.75% interest rate.");
} else {
System.out.println("9.5% interest rate.");
}
} else {
System.out.println("8% interest rate.");
}
- If condition 1 is true, then condition 2 is checked. if this condition is true, then the statement(s) following the 'if' statement is executed. if condition 2 evaluates to false, then the statements in the else block are executed.
- If condition 1 is not true, then the statement in the outer 'else' block is executed.