When writing programs, the computer normally executes code line by line from top to bottom.
However, real programs often need to make decisions.
For example:
- Should a user be allowed to log in?
- Did a student pass or fail?
- What day of the week should be displayed?
The way programs control the order in which code runs is called control flow.
JavaScript provides several control flow structures that help us make decisions in code.
Real-Life Example of Control Flow
Think about a simple real-life decision.
You want to go outside.
- If it is raining → take an umbrella
- Else → go outside normally
Programming works in the same way. The program checks a condition, then decides what code to run.
The if Statement
The if statement runs a block of code only if a condition is true.
Syntax
if (condition) {
// code runs if condition is true
}
Example
let age = 20;
if (age >= 18) {
console.log("You are an adult");
}
Step-by-Step Execution
JavaScript checks the condition age >= 18
Since 20 >= 18 is true
The message "You are an adult" is printed
If the condition were false, the code inside the block would not run.
The if-else Statement
Sometimes we want two possible outcomes.
For example:
- If marks ≥ 40 → pass
- Otherwise → fail
This is where if-else is used.
Syntax
if (condition) {
// runs if condition is true
} else {
// runs if condition is false
}
Example
let marks = 35;
if (marks >= 40) {
console.log("You passed");
} else {
console.log("You failed");
}
Execution
JavaScript checks marks >= 40
35 >= 40 is false
The else block runs
Output:
You failed
The else if Ladder
Sometimes we need multiple conditions.
Example:
- Marks ≥ 90 → Grade A
- Marks ≥ 75 → Grade B
- Marks ≥ 50 → Grade C
- Otherwise → Fail
This is called an else-if ladder.
Example
let marks = 78;
if (marks >= 90) {
console.log("Grade A");
}
else if (marks >= 75) {
console.log("Grade B");
}
else if (marks >= 50) {
console.log("Grade C");
}
else {
console.log("Fail");
}
How It Works
JavaScript checks conditions one by one:
marks >= 90 → false
marks >= 75 → true
JavaScript prints Grade B
The remaining conditions are skipped
The switch Statement
A switch statement is useful when you want to compare one value against multiple possible cases.
Syntax
switch(value) {
case option1:
// code
break;
case option2:
// code
break;
default:
// code if no match
}
Example: Day of the Week
let day = 3;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
default:
console.log("Invalid day");
}
Output:
Wednesday
Why break Is Important in Switch
The break statement stops execution after a case runs.
Without break, JavaScript will continue executing the next cases.
Example without break:
let day = 1;
switch(day) {
case 1:
console.log("Monday");
case 2:
console.log("Tuesday");
}
Output:
Monday
Tuesday
This happens because the program falls through to the next case.
Using break prevents this.
When to Use switch vs if-else
Use if-else when:
- Conditions involve ranges
- Logical comparisons are needed
Example:
marks > 40
age >= 18
temperature < 10
Use switch when:
- You compare one variable
- It has multiple specific values
Example:
day = Monday, Tuesday, Wednesday
menu option = 1, 2, 3
role = admin, user, guest
Assignment Practice
1️⃣ Check if a Number Is Positive, Negative, or Zero
2️⃣ Print the Day of the Week Using switch
Solution to 1.
let num = -5;
if (num > 0) {
console.log("Positive number");
}
else if (num < 0) {
console.log("Negative number");
}
else {
console.log("Zero");
}
Explanation
An else-if ladder is used because we need to check multiple conditions.
Solution to 2.
let day = 5;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
default:
console.log("Invalid day");
}
Explanation
A switch statement is used because the variable day can match specific fixed values.
Top comments (0)