Coding/Learn/java/if statements
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnIf Statements
13 / 31 0%
Lesson 13 of 31
5 mins read

If Statements

PreviousNext Lesson
In Plain English

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.

The Mental Model

"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 { }.

PointCore Rules to Remember

Point star
Strict Boolean Condition: Java requires a boolean expression inside if (...).
Point star
Block Scope: Variables declared inside the if block are destroyed when the block ends.
Point star
Always Use Braces: Omitting braces can lead to subtle bugs when adding lines later.

Common Beginner Traps & How to Fix Them

Mistake: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.

Pro Tip: In clean code, avoid comparing booleans explicitly with == true: use if (isActive) instead of if (isActive == true).

Live Interactive Example

Hit Run Code to see it live
Conditional Alert Trigger
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: Because 15 < 20 is true, the warning message executes between the checks.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Apply free shipping rule

  • PointGiven orderTotal = 65, check if orderTotal is greater than or equal to 50.
  • PointIf true, print "Free Shipping Applied!".
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates