Control Flow

Coordinate sequential, parallel, and conditional asynchronous operations.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What is Control Flow? Why is it important? if , else , else if switch Ternary Operator break and continue Real-world examples Interview questions What is Control Flow? Control Flow determines the order in which JavaScript executes your code . Simple Definition: Control Flow decides which code runs, when it runs, and how many times it runs. Real-Life Example 🚦 Imagine you're driving a car. Traffic Light │ ▼ Green → Go Yellow → Slow Down Red → Stop You make a decision based on the traffic light. JavaScript works the same way. Example Without Control Flow console . log( "Wake Up" ); console . log( "Brush Teeth" ); console . log( "Go to Office" ); Output Wake Up Brush Teeth Go to Office The code runs from top to bottom. 1. if Statement Run code only if a condition is true. const age = 20 ; if ( age >= 18 ) { console . log( "You can vote." ); } Output You can vote. 2. if...else Choose between two options. const age = 16 ; if ( age >= 18 ) { console . log( "Eligible" ); } else { console . log( "Not Eligible" ); } Output Not Eligible 3. if...else if...else Check multiple conditions. const marks = 75 ; if ( marks >= 90 ) { console . log( "Grade A" ); } else if ( marks >= 70 ) { console . log( "Grade B" ); } else if ( marks >= 50 ) { console . log( "Grade C" ); } else { console . log( "Fail" ); } Output Grade B Real-Life Example 🎓 Marks 90+ → Grade A 70+ → Grade B 50+ → Grade C Below 50 → Fail 4. switch Statement Use switch when checking one value against many options. const day = "Monday" ; switch ( day ) { case "Monday" : console . log( "Start Work" ); break ; case "Sunday" : console . log( "Holiday" ); break ; default : console . log( "Normal Day" ); } Output Start Work Why break ? Without break , JavaScript continues executing the next cases. const day = "Monday" ; switch ( day ) { case "Monday" : console . log( "Monday" ); case "Tuesday" : console . log( "Tuesday" ); } Output Monday Tuesday This is called fall-through . 5. Ternary Operator Short form of if...else . Normal const age = 20 ; if ( age >= 18 ) { console . log( "Adult" ); } else { console . log( "Minor" ); } Ternary const age = 20 ; const result = age >= 18 ? "Adult" : "Minor" ; console . log( result ); Output Adult 6. break Stops a loop immediately. for ( let i = 1 ; i <= 5 ; i ++ ) { if ( i === 3 ) { break ; } console . log( i ); } Output 1 2 7. continue Skips the current iteration. for ( let i = 1 ; i <= 5 ; i ++ ) { if ( i === 3 ) { continue ; } console . log( i ); } Output 1 2 4 5 Real-Life Example 🛒 Shopping Cart const total = 6000 ; if ( total >= 5000 ) { console . log( "Free Delivery" ); } else { console . log( "Delivery Charges" ); } Output Free Delivery Another Example Login System const isLoggedIn = true ; if ( isLoggedIn ) { console . log( "Welcome!" ); } else { console . log( "Please Login" ); } Output Welcome! Comparison Statement Use Case if One condition if...else Two choices else if Multiple conditions switch Many values of one variable ? : Short if...else break Stop loop or switch continue Skip current loop iteration Interview Question What is the output? let num = 10 ; if ( num > 5 ) { console . log( "A" ); } else { console . log( "B" ); } Output A When Should You Use What? Situation Best Choice One condition if Two options if...else Multiple conditions if...else if One variable, many values switch Short condition Ternary ( ? : ) Stop a loop break Skip one iteration continue Key Points ✅ Control Flow decides how your program executes. ✅ Use if for one condition. ✅ Use if...else for two choices. ✅ Use switch for many possible values of one variable. ✅ Use the ternary operator for simple conditions. ✅ break stops execution. ✅ continue skips the current loop iteration. 🧠 Easy Formula Need to make a decision? │ ▼ if / else Many values? │ ▼ switch Short condition? │ ▼ Ternary (? :) Stop loop? │ ▼ break Skip one iteration? │ ▼ continue Remember: Control Flow controls the path your program follows based on conditions and loops. It's what makes your programs dynamic and able to respond to different situations.