Saturday, June 14, 2025

Javascript Module 7

 Module 7 : Javascript Control Flow: if, else, switch 

πŸ” Introduction to Control Flow

Control flow in JavaScript refers to the order in which individual statements, instructions, or function calls are executed or evaluated. JavaScript executes code line-by-line (top-down) by default. However, control flow structures like if, else, and switch allow us to change that flow depending on conditions.


1️⃣ if Statement



πŸ”Ή Syntax:

javascript

code

if (condition) { // code block to execute if condition is true }

πŸ”Ή Explanation:

The condition is evaluated.

If it evaluates to true, the block inside the if is executed.

Otherwise, it's skipped.

✅ Example:

javascript

code

let age = 18; if (age >= 18) { console.log("You are eligible to vote."); }

Output:

You are eligible to vote.


2️⃣ if...else Statement

πŸ”Ή Syntax:

javascript

code

if (condition) { // true block } else { // false block }

✅ Example:

javascript

code

let temperature = 30; if (temperature > 35) { console.log("It's very hot outside."); } else { console.log("The temperature is moderate."); }

Output:

The temperature is moderate.


3️⃣ if...else if...else Statement

πŸ”Ή Syntax:

javascript

 code

if (condition1) { // code block } else if (condition2) { // code block } else { // fallback code block }

✅ Example:

javascript

 code

let score = 85; if (score >= 90) { console.log("Grade: A"); } else if (score >= 75) { console.log("Grade: B"); } else { console.log("Grade: C"); }

Output:

Grade: B


4️⃣ switch Statement




πŸ”Ή Syntax:

javascript

code

switch (expression) { case value1: // code block break; case value2: // code block break; default: // default block }

πŸ”Ή Explanation:

The expression is evaluated.

The code executes the matching case.

break stops the execution from falling into the next case.

default is executed if no cases match.

✅ Example:

javascript

 code

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"); }

Output:

Wednesday


πŸ”¬EXPERIMENT

Write a program to determine the type of a triangle based on side lengths using if...else and switch.

✅ Example Code:

javascript

code

function checkTriangleType(a, b, c) { if (a <= 0 || b <= 0 || c <= 0) { console.log("Invalid side lengths"); } else if (a === b && b === c) { console.log("Equilateral triangle"); } else if (a === b || b === c || a === c) { console.log("Isosceles triangle"); } else { console.log("Scalene triangle"); } let triangleType; switch (true) { case (a === b && b === c): triangleType = "Equilateral"; break; case (a === b || b === c || a === c): triangleType = "Isosceles"; break; default: triangleType = "Scalene"; } console.log("Triangle type (via switch):", triangleType); } checkTriangleType(5, 5, 5);

Output:

go

 code

Equilateral triangle

Triangle type (via switch): Equilateral



πŸ” Explanation:

The if...else determines triangle type based on conditions.

switch uses true as the expression to evaluate each condition logically.

 how both structures can be used together.


🧠 Exercises with Explanations

πŸ”Έ Exercise 1:

Write a program to determine if a number is positive, negative, or zero using if...else.

Solution:

javascript

code

let num = -10; if (num > 0) { console.log("Positive number"); } else if (num < 0) { console.log("Negative number"); } else { console.log("Zero"); }


πŸ”Έ Exercise 2:

Create a simple menu using switch:

javascript

 code

// Input: 1 - Start Game, 2 - Load Game, 3 - Exit let option = 2; switch (option) { case 1: console.log("Starting new game..."); break; case 2: console.log("Loading game..."); break; case 3: console.log("Exiting..."); break; default: console.log("Invalid option"); }


πŸ“š  Insights : When to Use What?




Use Case

Use if...else

Use switch

Evaluating ranges (e.g., x > 10)

Multiple exact matches (e.g., values of a variable)

⚠️ (can become long)

Logical conditions (&&, `



`)

Cleaner readability for many exact options


Advanced Use Case:

javascript

 code

let status = "approved"; switch (status) { case "pending": case "waiting": console.log("Status: Awaiting review"); break; case "approved": console.log("Status: Approved!"); break; case "rejected": console.log("Status: Rejected."); break; default: console.log("Unknown status"); }


πŸ“˜ Summary

if is for decision making based on boolean expressions.

if...else provides a fallback block.

else if allows multiple conditions.

switch is better for multiple discrete values.

Mixing if and switch can solve complex logic problems.


πŸ§‘‍πŸ”¬ Final Experiment












Create a Student Grading System 

using both if and switch.

javascript

code

function gradeStudent(marks) { let grade; if (marks >= 90) { grade = 'A'; } else if (marks >= 80) { grade = 'B'; } else if (marks >= 70) { grade = 'C'; } else if (marks >= 60) { grade = 'D'; } else { grade = 'F'; } switch (grade) { case 'A': console.log("Excellent!"); break; case 'B': console.log("Very good"); break; case 'C': console.log("Good"); break; case 'D': console.log("Needs Improvement"); break; case 'F': console.log("Failed"); break; } console.log("Final Grade:", grade); } gradeStudent(87);

Output:

less

 code

Very good

Final Grade: B




No comments:

Post a Comment

Javascript Module 13

  Javascript Module 13 If You want To Earn Certificate For My  All   Course Then Contact Me At My  Contact  Page   then I Will Take A Test A...