🔁 Day 4 — Conditional Statements (if, else, switch)
Welcome to Day 4 🚀 Today we learn how to make your programs **smart** — to think, decide, and act based on conditions. Conditional statements help you execute different code blocks depending on whether something is true or false.
⚙️ 1️⃣ The if Statement
The if statement checks a condition. If it’s true, the code inside runs.
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
}
🔄 2️⃣ if...else
Use else when you want to run one block if true, another if false.
let age = 16;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("Sorry, you are too young to vote.");
}
🔁 3️⃣ if...else if...else
Used for multiple conditions.
let marks = 85;
if (marks >= 90) {
console.log("Grade: A+");
} else if (marks >= 75) {
console.log("Grade: A");
} else if (marks >= 60) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
🔀 4️⃣ switch Statement
The switch statement is a cleaner way to handle multiple conditions based on one variable.
let day = 3;
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
case 3: console.log("Wednesday"); break;
default: console.log("Invalid Day");
}
🧩 5️⃣ Ternary Operator
A short way to write if...else in one line.
let age = 20; let message = (age >= 18) ? "Adult" : "Minor"; console.log(message);
🧠 6️⃣ Practice Task
- Write a program to check if a number is positive, negative, or zero.
- Use
switchto print the month name for a given number. - Create a ternary expression to check if a person can drive (age ≥ 18).
🎯 Summary
Today, you learned how to control your program’s logic with conditions. Tomorrow, you’ll move into **Loops** — where your program starts repeating intelligently.
Comments
Post a Comment