What is an If Else Statement in Programming? Beginner’s Guide

Should the user see this page or get redirected? Is the password correct or not? Does the cart have items or is it empty?

Every one of those decisions is handled by an if/else statement. It is one of the most important concepts in all of coding — and one of the simplest to understand once you see it in action.

🔀 If / Else

What Is an If/Else Statement?

An if/else statement tells your program to do one thing if a condition is true — and something else if it is not. One condition. Two possible paths. Your code picks one and runs it.
🚦 Analogy

Real-Life Analogy — Traffic Lights

Think of a traffic light. If the light is green — you go. If it is not green — you stop. No maybe. Just true or false — and a clear action for each.

If light === “green” → console.log(“Go!”)
🛑
Else → console.log(“Stop!”)
📋 Step by Step

How If/Else Works Step by Step

1
Program reaches an if statement
2
It checks the condition inside the brackets
3
Condition is true → runs the if block
4
Condition is false → skips if, runs else block
5
Program continues normally after both blocks
⚡ traffic.js
let light = “green”;

if (light === “green”) {
  console.log(“Go!”); // ✓ runs this
} else {
  console.log(“Stop!”); // skipped
}
📋 Basic If

The Basic If Statement

The simplest form of decision making in code.

The if statement runs a block of code only when a condition is true. If the condition is false — nothing happens and the program moves on.

⚡ JavaScript

Syntax in JavaScript

if (condition) {
  // code runs only if condition is true
}
⚡ check.js
let age = 20;

if (age >= 18) {
  console.log(“Access granted”); // ✓ runs — age is 20
}
🐍 Python

Syntax in Python

Python uses a colon and indentation instead of curly braces — no brackets around the condition either.

🐍 check.py
age = 20

if age >= 18:
  print(“Access granted”) # ✓ runs — age is 20
⚡ JavaScript
Uses ( ) around condition
Uses { } for the block
🐍 Python
No ( ) needed
Uses : + indentation
❌ When False

What Happens When Condition is False?

With just an if and no else — nothing happens when the condition is false. The program skips that block and continues to the next line.

⚡ false-case.js
let age = 15; ✓ Runs
if (age >= 18) { ✓ Checked
  console.log(“Access granted”); ✗ Skipped
} ✓ Runs
console.log(“Program continues”); ✓ Always
Code after the if block always runs — regardless of whether the condition was true or false. Only the code inside the curly braces gets skipped.
🔀 Two Paths

If/Else — Two Paths

A basic if only runs when true. But what about false? The else block catches that. Together they give your program exactly two paths — one for true, one for false.

➕ Else Block

Adding the Else Block

The else block runs when the if condition is false. You never need to write another condition — it just catches everything the if did not.

🔍 if (condition) ?
✅ TRUE
if block
runs this ✓
❌ FALSE
else block
runs this ✓
if (condition) {
  // runs when TRUE
} else {
  // runs when FALSE
}
💻 Real Example

Real Code Example

A simple login check — either the password is correct or it is not. No third option.

⚡ JavaScript
🐍 Python
let password = “abc123”;

if (password === “abc123”) {
  console.log(“Welcome! You are logged in.”);
} else {
  console.log(“Wrong password. Try again.”);
}
Output Welcome! You are logged in.
password = “abc123”

if password == “abc123”:
  print(“Welcome! You are logged in.”)
else:
  print(“Wrong password. Try again.”)
Output Welcome! You are logged in.
One of two things will always happen. Either the if block runs or the else block runs — never both, never neither.
🪆

Nested If/Else — Conditions Inside Conditions

Check something — then check something else inside that.

Sometimes one condition is not enough. You need to check something — and then check something else inside that. That is called a nested if — an if statement inside another if statement.

⏰ When to Use

When to Use Nested If

Use nested if when the second check only makes sense if the first one already passed.

🔵 First Check — Outer
age >= 18 ?
🟢 Second Check — Inner
hasLicense === true ?
✅ Both pass → You can drive!
⚠️ Age ok, no license → Get a license first
❌ Age fails → Too young to drive
💻 Example

Simple Nested Example

⚡ drive.js
let age = 20;
let hasLicense = true;

if (age >= 18) {
  // first check passed — now check license
  if (hasLicense === true) {
    console.log(“You can drive!”);
  } else {
    console.log(“You need a license first.”);
  }
} else {
  console.log(“You are too young to drive.”);
}
⚠️ Don’t go too deep. If you find yourself writing three or four levels of nested ifs — there is usually a cleaner way to write it.
⛓️ Else If

Else If — Checking Multiple Conditions

Chain multiple conditions together — one after another.

Sometimes two paths are not enough. What if you need three, four, or five different outcomes? That is where else if comes in — it lets you chain multiple conditions together.

⚡ JavaScript

Using else if in JavaScript

The program checks each condition from top to bottom. The first one that is true wins — everything else is skipped.

⚡ syntax.js
if (first condition) {
  // runs if first is true
} else if (second condition) {
  // runs if second is true
} else {
  // runs if nothing matched
}
🐍 Python

Using elif in Python

Python uses elif instead of else if — but it works exactly the same way.

🐍 syntax.py
if first_condition:
  # runs if first is true
elif second_condition:
  # runs if second is true
else:
  # runs if nothing matched
⚡ JavaScript uses
else if
🐍 Python uses
elif
🎓 Real Example

Real-Life Grade Checker Example

A score >= 90 Excellent!
B score >= 75 Good job! score = 75 ✓
C score >= 60 Passing
F below 60 Please retry
⚡ grades.js
let score = 75;

if (score >= 90) {
  console.log(“A — Excellent!”);
} else if (score >= 75) {
  console.log(“B — Good job!”); // ✓ this runs
} else if (score >= 60) {
  console.log(“C — Passing”);
} else {
  console.log(“F — Please retry”);
}
Output B — Good job!
💡 Top to bottom: The program checks A first, then B — score 75 matches B so it stops there. C and F are never checked.
⚡ Ternary

The Ternary Operator — One-Line If/Else

A shortcut for simple if/else — no curly braces needed.

Sometimes your if/else is simple enough to write in a single line. The ternary operator lets you do exactly that — no curly braces, no extra lines.

📐 Syntax

Ternary Syntax

The ternary operator has three parts — that is why it is called “ternary.”

🔍 Anatomy of a Ternary:
condition
Check this
?
If true
value if true
Return this
:
If false
value if false
Return this
⚡ ternary.js
// Regular if/else — 5 lines
let age = 20;
if (age >= 18) {
  console.log(“Adult”);
} else {
  console.log(“Minor”);
}
⚡ Same thing — one line
⚡ ternary.js
let status = age >= 18 ? “Adult” : “Minor”;
console.log(status);
Output Adult
✅ When to Use

When to Use It

Use it when the condition is simple and you just want to assign one of two values
Use it when the result fits comfortably on one line
Avoid it when the condition is complex or either value needs explanation
Avoid it when nesting — ternary inside ternary becomes unreadable fast
💡 Readable code is always better than clever code. If the ternary makes your code harder to read — use a regular if/else instead.
💻 Real Examples

Real-Life If/Else Examples

Three examples you will actually build as a beginner.

01
🎂

Age Checker

⚡ age.js
let age = 17;

if (age >= 18) {
  console.log(“Access granted”);
} else {
  console.log(“You must be 18 or older”);
}
Output You must be 18 or older
Uses: if/else >= operator
02
🔐

Login Validator

⚡ login.js
let username = “sara”;
let password = “pass123”;

if (username === “sara” && password === “pass123”) {
  console.log(“Welcome back, Sara!”);
} else {
  console.log(“Invalid credentials”);
}
Output Welcome back, Sara!
Uses: if/else === operator && (AND)
03
🛒

Shopping Discount System

⚡ discount.js
let cartTotal = 120;

if (cartTotal >= 200) {
  console.log(“20% discount applied!”);
} else if (cartTotal >= 100) {
  console.log(“10% discount applied!”); // ✓ runs
} else {
  console.log(“No discount — spend more!”);
}
Output 10% discount applied!
Uses: if/else if/else >= operator

FAQs About If/Else

Quick answers to the questions beginners ask most

Q 01

What is the difference between if and else if?

+
if starts a new condition check — it always runs first. else if only runs when the previous condition was false. You can have one if — but as many else if blocks as you need after it.
if Always first. Starts the chain — checked every time
else if Only if previous was false. Can add as many as needed
else Catches everything left. Runs when nothing else matched
Think of if as the first door and else if as extra doors — only opened if the ones before them were locked.
Q 02

Can you have an if without an else?

+
Yes — absolutely. The else block is completely optional. If nothing needs to happen when the condition is false — just write the if by itself.
✅ if only — valid
if (isLoggedIn) {
  showDashboard();
}
// no else needed
Program just skips the block if false
✅ if + else — also valid
if (isLoggedIn) {
  showDashboard();
} else {
  showLogin();
}
When you need both paths handled
💡 Use else only when you actually need something to happen in the false case. Don’t add it just to have it.
Q 03

What is the difference between elif and else if?

+
They do exactly the same thing — just in different languages. The logic, behavior, and purpose are identical.
⚡ JavaScript uses
else if
Two separate words
🐍 Python uses
elif
One word — shorter
⚡ JS vs 🐍 Python
// JavaScript
if (x > 10) { … } else if (x > 5) { … }

# Python
if x > 10: … elif x > 5: …
If you learn one, you already understand the other. Just remember which language uses which spelling.