if / else
Try it yourself →Executes code based on a condition.
Syntax
if (condition) { } else { }Description
Condition in parentheses, blocks in { }. Intermediate conditions with else if.
Example
public class Main {
public static void main(String[] args) {
int score = 75;
if (score >= 50) {
System.out.println("Passed");
} else {
System.out.println("Failed");
}
}
}Expected output
Passed
When to use?
Flow branching.
Common mistakes
- ⚠️ String equality is checked with .equals(), not ==.