if-else Statements in Java
Swipe to show menu
So far, you've learned how to compare values and combine conditions. But the program always just calculates a result — it doesn't really choose what to do with it.
In real systems, that's not enough. A coffee shop doesn't just check if something is true — it also reacts:
- If the shop is open → accept orders;
- If it's closed → reject orders;
- If a condition is met → show a special message.
That's exactly what if-else statements allow us to do.
An if-else statement lets the program choose between two paths depending on whether a condition is true or false:
if (condition) {
// runs if condition is true
} else {
// runs if condition is false
}
Example 1 — Boolean variable as condition
Main.java
1234567891011121314package com.example; public class Main { public static void main(String[] args) { boolean isMember = false; if (isMember) { System.out.println("You get a loyalty discount"); } else { System.out.println("No discount available"); } } }
The isMember variable is used directly as the condition. Since it's false, Java skips the if block and runs the else block instead, printing "No discount available".
Example 2 — Expression as condition
Main.java
12345678910111213package com.example; public class Main { public static void main(String[] args) { int orderPrice = 7; if (orderPrice > 5) { System.out.println("This order is expensive"); } else { System.out.println("This order is affordable"); } } }
Here Java first evaluates the expression orderPrice > 5, which returns true, so the if block runs and prints "This order is expensive".
With if-else, the program stops being passive and starts taking one of two possible paths depending on the condition.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
if-else Statements in Java
So far, you've learned how to compare values and combine conditions. But the program always just calculates a result — it doesn't really choose what to do with it.
In real systems, that's not enough. A coffee shop doesn't just check if something is true — it also reacts:
- If the shop is open → accept orders;
- If it's closed → reject orders;
- If a condition is met → show a special message.
That's exactly what if-else statements allow us to do.
An if-else statement lets the program choose between two paths depending on whether a condition is true or false:
if (condition) {
// runs if condition is true
} else {
// runs if condition is false
}
Example 1 — Boolean variable as condition
Main.java
1234567891011121314package com.example; public class Main { public static void main(String[] args) { boolean isMember = false; if (isMember) { System.out.println("You get a loyalty discount"); } else { System.out.println("No discount available"); } } }
The isMember variable is used directly as the condition. Since it's false, Java skips the if block and runs the else block instead, printing "No discount available".
Example 2 — Expression as condition
Main.java
12345678910111213package com.example; public class Main { public static void main(String[] args) { int orderPrice = 7; if (orderPrice > 5) { System.out.println("This order is expensive"); } else { System.out.println("This order is affordable"); } } }
Here Java first evaluates the expression orderPrice > 5, which returns true, so the if block runs and prints "This order is expensive".
With if-else, the program stops being passive and starts taking one of two possible paths depending on the condition.
Thanks for your feedback!