Java If Statements
The `if` statement is Java's fundamental decision-making construct. It tests a condition inside parentheses: if the condition evaluates to `true`, the statements enclosed within its curly braces execute; otherwise, Java skips the block entirely.
"An if statement is like an automatic door sensor. If the sensor detects motion (true), the door opens. If no motion is detected (false), the door stays closed and nothing changes."
Deep Dive: How It Works
Syntax: `if (condition) { /* statements */ }`
The condition inside the parentheses MUST evaluate strictly to a boolean (`true` or `false`). Passing numbers like `if (1)` will throw a compile error.
Always wrap the body in curly braces `{ }`, even for single statements, to avoid dangling statement bugs.
Syntax Blueprint
if (condition) {
// Code runs only when condition is true
}Condition must be inside parentheses ( ). Statements go inside curly braces { }.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Placing a semicolon after the if condition: if (score > 50); { ... }Why it happens: The semicolon terminates the if statement immediately, making the block run unconditionally.
How to fix: Never place a semicolon directly after the closing parenthesis of an if statement.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Apply free shipping rule
Given orderTotal = 65, check if orderTotal is greater than or equal to 50.
If true, print "Free Shipping Applied!".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.