JavaCore java
If else in Java
Conditional Statements
- Decision-making statements in Java.
- Used to control program flow based on conditions.
General Form:
if (condition) {
// executes if condition is true
} else {
// executes if condition is false
}
Flowchart Representation
[Condition?]
/ \
True False
/ \
[Execute Block1] [Execute Block2]
Example – Car Speed
- If speed is greater than allowed → decrease
- If speed is less than allowed → increase
- If equal → stop / maintain speed
Example 1 – Basic If
public class Demo {
public static void main(String[] args) {
int x = 18;
if (x > 10) {
System.out.println("Hello");
}
}
}
- If condition is
true
→ block executes. - If condition is
false
→ block is skipped.
Example 2 – If with Else
int x = 8;
if (x > 10 && x <= 20) { // 11 - 20
System.out.println("Hello");
} else {
System.out.println("Bye");
}
Note:
if
can be used alone.else
cannot be used withoutif
.
Example 3 – Comparing Two Numbers
int x = 5;
int y = 7;
if (x > y) {
System.out.println(x);
} else {
System.out.println(y);
}
👉 Output: 7
Example 4 – Braces are Important
if (x > y)
System.out.println(x);
System.out.println("Thank you"); // Always executes, not part of if
else
System.out.println(y); // ❌ Error: 'else' without 'if'
✅ Correct way:
if (x > y) {
System.out.println(x);
System.out.println("Thank you");
} else {
System.out.println(y);
}
Key Notes
- Java does not depend on indentation (unlike Python).
- Always use
{}
if more than one statement in a block. - Helps avoid logical errors.