Add Conditions
Learning Objectives
- Understand conditional logic
- Master
if,else if, andelsestatements - Learn the ternary operator
- Use
switchstatements - Apply conditions to real-world problems
Section 4.1: What's a Condition?
Conditional Logic
A condition is a statement that is either true or false. We use conditions to make decisions in our code.
let age = 18;
if (age >= 18) {
console.log("You are an adult");
}
If Statement
The if statement executes code only if a condition is true:
if (condition) {
// Code executes if condition is true
}
Example: Checking Age
let age = 20;
if (age >= 18) {
console.log("You can vote");
}
If-Else Statement
Either execute one block OR another:
if (condition) {
// Execute if true
} else {
// Execute if false
}
Example: Even or Odd
let number = 7;
if (number % 2 === 0) {
console.log("Even number");
} else {
console.log("Odd number");
}
Section 4.2: Alternative Conditions
Comparison Operators
let x = 10, y = 5;
console.log(x > y); // true (greater than)
console.log(x < y); // false (less than)
console.log(x >= y); // true (greater or equal)
console.log(x <= y); // false (less or equal)
console.log(x === y); // false (equal value and type)
console.log(x !== y); // true (not equal)
Strict vs Loose Equality
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality)
// Always use === in JavaScript
Logical Operators
Both conditions must be true:
let age = 25;
let hasLicense = true;
if (age >= 18 && hasLicense) {
console.log("You can drive");
}
At least one condition must be true:
let isWeekend = true;
let isHoliday = false;
if (isWeekend || isHoliday) {
console.log("No work today!");
}
Reverses the condition:
let isRaining = true;
if (!isRaining) {
console.log("Go outside");
} else {
console.log("Stay inside");
}
Section 4.3: Add Additional Logic
If-Else If-Else Chain
Handle multiple conditions:
let score = 75;
let grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}
console.log("Grade: " + grade);
Nested Conditions
Conditions inside conditions:
let age = 25;
let hasLicense = true;
if (age >= 18) {
if (hasLicense) {
console.log("You can drive");
} else {
console.log("You need a license");
}
} else {
console.log("You must be 18 to drive");
}
Ternary Operator (Conditional Expression)
Shorthand for simple if-else:
// Standard if-else
let status;
if (age >= 18) {
status = "adult";
} else {
status = "minor";
}
// Using ternary operator
let status = age >= 18 ? "adult" : "minor";
Syntax: condition ? valueIfTrue : valueIfFalse
let message = score >= 50 ? "Passed" : "Failed";
let access = isAdmin ? "Full access" : "Limited access";
let discount = quantity > 10 ? "20%" : "10%";
Section 4.4: Multiple Choices
Switch Statement
Use switch when you have many specific options:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
default:
dayName = "Weekend";
}
console.log(dayName); // Wednesday
Switch with Multiple Cases
let grade = "B";
switch (grade) {
case "A":
case "B":
console.log("Excellent work!");
break;
case "C":
console.log("Good work!");
break;
case "D":
case "F":
console.log("Need improvement");
break;
default:
console.log("Invalid grade");
}
If vs Switch
Use if-else for:
- Complex conditions
- Range comparisons (>, <, >=, <=)
- Boolean logic (&&, ||)
Use switch for:
- Exact matches
- Many specific values
- Cleaner code with multiple cases
Coding Challenges
Challenge 4.1: Following Day
Task: Write a program that:
- Asks for a day number (1-7)
- Displays the day name and the next day
Example Output:
Enter day (1-7): 3
Today: Wednesday
Tomorrow: Thursday
Challenge 4.2: Number Comparison
Task: Compare two numbers and display:
- Which is larger
- Which is smaller
- If they're equal
Example Output:
Enter first number: 10
Enter second number: 25
25 is larger
10 is smaller
Challenge 4.3: Number of Days in a Month
Task: Ask for a month number and display how many days it has
- Months with 31 days: 1, 3, 5, 7, 8, 10, 12
- Months with 30 days: 4, 6, 9, 11
- February: 28 days (29 in leap years)
Challenge 4.4: Adding Seconds to Time
Task: Convert seconds into hours, minutes, and seconds
Example:
Input: 3661 seconds
Output: 1 hour, 1 minute, 1 second
Key Takeaways
✅ Conditions control program flow
✅ Use === for strict equality comparisons
✅ Combine conditions with logical operators (&&, ||, !)
✅ Chain conditions with else if
✅ Use ternary operator for simple decisions
✅ Use switch for multiple specific values
Quiz Questions
- What does
===do differently from==? - Explain the AND (&&) operator with an example
- When would you use a
switchinstead ofif-else? - What does the ternary operator do?
- How do you check if a number is between 10 and 20?
Next Module: Module 5 - Repeat Statements