🔀 Control Flow Statements
Last Updated: 9th October 2025
Control Flow Statements decide which part of your program will run, when, and how many times. They give your program the ability to make decisions and repeat tasks.
Hinglish Tip 🗣: Control Flow ka kaam hai decide karna ki program kaunsa step pehle chalega, kaunsa baad me, kisko repeat karega aur kaunsa skip karega.
Why Control Flow is Important?
Without control flow, JavaScript would just run your code line-by-line from top to bottom. Control flow lets you:
- Run different code based on a condition.
- Repeat code multiple times.
- Skip or stop parts of a loop.
Real-life Analogy:
- Conditional: If it’s raining, take an umbrella.
- Loop: Walk until you reach your destination.
- Jump: If you see danger, stop immediately.
Main Categories
- Conditional Statements — Decision making.
- Looping Statements — Repeat a block of code.
- Jump/Transfer Statements — Change the normal sequence of execution.
| Category | Keywords | Purpose |
|---|---|---|
| Conditional | if, else if, else, switch | Run different code based on a condition |
| Looping | for, while, do...while, for...in, for...of | Repeat code multiple times |
| Jump | break, continue | Skip or stop parts of a loop |
Code Previews
Conditional Example:
let x = 10;
if (x > 0) {
// Conditional
console.log("Positive number");
} else {
console.log("Non-positive number");
}
// switch example
let color = "red";
switch (color) {
case "red":
console.log("Stop!");
break;
case "green":
console.log("Go!");
break;
default:
console.log("Caution!");
}
Loop Example:
for (let i = 0; i < 3; i++) {
console.log("Loop iteration:", i);
}
let j = 0;
while (j < 3) {
console.log("While loop:", j);
j++;
}
Jump Example:
for (let i = 0; i < 5; i++) {
if (i === 2) continue; // Skip iteration
if (i === 4) break; // Stop loop
console.log(i);
}
💡 Don’t worry! We’ll explore each of these in full detail in the next sections.