Coding/Learn/java/if else
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-Else Statements
14 / 31 0%
Lesson 14 of 31
5 mins read

If-Else Statements

PreviousNext Lesson
In Plain English

When you need to perform one action when a condition is met and an alternative action when it is not, the `if-else` statement provides a clean two-way fork. Exactly one of the two blocks is guaranteed to execute.

The Mental Model

"An if-else statement is a fork in the road with a signpost: if you have a VIP pass, take the left path; otherwise (else), take the standard path. You cannot take both."

Deep Dive: How It Works

Syntax: `if (condition) { ... } else { ... }`

The `else` keyword has no condition attached to it; it acts as the default fallback whenever the `if` condition evaluates to `false`.

The ternary operator `condition ? expr1 : expr2` is a concise inline alternative for simple value assignments.

Syntax Blueprint

if (condition) {
    // True branch
} else {
    // False branch
}

Only one branch will ever execute.

PointCore Rules to Remember

Point star
Guaranteed Branch Execution: Exactly one branch (if or else) will run.
Point star
Ternary Alternative: String status = score >= 50 ? "Pass" : "Fail";
Point star
Clean Fallback: Else guarantees safe default behavior.

Common Beginner Traps & How to Fix Them

Mistake:Adding a condition to else: else (x < 10) { ... }

Why it happens: Plain else cannot have a condition. Only else if accepts a condition.

How to fix: Use "else if (x < 10)" or plain "else".

Pro Tip: Use the ternary operator for short single-line variable assignments to keep code concise.

Live Interactive Example

Hit Run Code to see it live
Authentication Status Check
Java 13
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: Since isLoggedIn is false, Java skips the if block and executes the else branch.

Your Turn: Micro Challenge

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

Micro Exercise

Check even or odd number

  • PointGiven number = 14, check if number % 2 == 0.
  • PointIf true, print "Even Number". Otherwise, print "Odd Number".
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