<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Control Flow in JavaScript: If, Else, and Switch Explained]]></title><description><![CDATA[Control Flow in JavaScript: If, Else, and Switch Explained]]></description><link>https://if-else-and-switch-control-flow-in-javascript.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>Control Flow in JavaScript: If, Else, and Switch Explained</title><link>https://if-else-and-switch-control-flow-in-javascript.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 25 Jun 2026 19:50:48 GMT</lastBuildDate><atom:link href="https://if-else-and-switch-control-flow-in-javascript.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Control Flow in JavaScript: if, else, and switch Explained]]></title><description><![CDATA[Who is this for? If you are just starting with JavaScript and want to understand how your program makes decisions — checking conditions and choosing what to do — this guide walks you through everythin]]></description><link>https://if-else-and-switch-control-flow-in-javascript.hashnode.dev/control-flow-in-javascript-if-else-and-switch-explained</link><guid isPermaLink="true">https://if-else-and-switch-control-flow-in-javascript.hashnode.dev/control-flow-in-javascript-if-else-and-switch-explained</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[chaicode webdev cohort 2026]]></category><category><![CDATA[if-else]]></category><category><![CDATA[Switch case]]></category><category><![CDATA[js]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[ABHISHEK KUMAR]]></dc:creator><pubDate>Wed, 29 Apr 2026 11:55:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6784893ff404b926e9fe3b72/50884988-4b56-4fe0-89fc-7643cbf36a48.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p><strong>Who is this for?</strong> If you are just starting with JavaScript and want to understand how your program makes decisions — checking conditions and choosing what to do — this guide walks you through everything from scratch.</p>
</blockquote>
<hr />
<h2>Table of Contents</h2>
<ol>
<li><p><a href="#1-what-is-control-flow">What Is Control Flow?</a></p>
</li>
<li><p><a href="#2-the-if-statement">The <code>if</code> Statement</a></p>
</li>
<li><p><a href="#3-the-if-else-statement">The <code>if-else</code> Statement</a></p>
</li>
<li><p><a href="#4-the-else-if-ladder">The <code>else if</code> Ladder</a></p>
</li>
<li><p><a href="#5-the-switch-statement">The <code>switch</code> Statement</a></p>
</li>
<li><p><a href="#6-when-to-use-switch-vs-if-else">When to Use switch vs if-else</a></p>
</li>
<li><p><a href="#diagrams">Diagrams</a></p>
</li>
<li><p><a href="#practice-assignment">Practice Assignment</a></p>
</li>
</ol>
<hr />
<h2>Introduction</h2>
<p>Every program makes decisions. Should the user be allowed to log in? Is this score high enough to pass? What day of the week is it?</p>
<p>In real life, you make hundreds of decisions every day without thinking — <em>"If it is raining, I will take an umbrella. Otherwise, I will leave it at home."</em> Control flow in programming is exactly the same idea: <strong>if this condition is true, do this — otherwise, do something else.</strong></p>
<p>JavaScript gives you clean, simple tools to express these decisions in code. Let us go through them one by one.</p>
<blockquote>
<p>💡 <strong>Open your browser console</strong> (<code>F12</code> → Console tab) and try every example as you read.</p>
</blockquote>
<hr />
<h2>1. What Is Control Flow?</h2>
<p>By default, JavaScript executes your code <strong>line by line, top to bottom</strong>:</p>
<pre><code class="language-javascript">console.log("Line 1");
console.log("Line 2");
console.log("Line 3");
// Output: Line 1 → Line 2 → Line 3 (always, in that order)
</code></pre>
<p><strong>Control flow</strong> is when your program <strong>changes direction</strong> based on a condition. Instead of always running every line, it chooses a path:</p>
<pre><code class="language-plaintext">Normal flow:       Control flow:
────────────       ─────────────────────────────
Line 1             Line 1
Line 2             Is the condition true?
Line 3               YES → run Line A, skip Line B
Line 4               NO  → skip Line A, run Line B
                   Line 4
</code></pre>
<p>Think of it like a road with a fork. The condition is the signpost — it tells your program which road to take.</p>
<h3>Real-life control flow examples</h3>
<table>
<thead>
<tr>
<th>Situation</th>
<th>Condition</th>
<th>What happens</th>
</tr>
</thead>
<tbody><tr>
<td>Entering a club</td>
<td>Age ≥ 18?</td>
<td>Yes → allowed in / No → turned away</td>
</tr>
<tr>
<td>Exam result</td>
<td>Marks ≥ 50?</td>
<td>Yes → pass / No → fail</td>
</tr>
<tr>
<td>Online order</td>
<td>Item in stock?</td>
<td>Yes → place order / No → show error</td>
</tr>
<tr>
<td>Traffic light</td>
<td>Light is red?</td>
<td>Yes → stop / No → go</td>
</tr>
</tbody></table>
<p>JavaScript uses <code>if</code>, <code>else</code>, <code>else if</code>, and <code>switch</code> to express all of these.</p>
<hr />
<h2>2. The <code>if</code> Statement</h2>
<p>The <code>if</code> statement is the most basic form of control flow. It runs a block of code <strong>only if</strong> a condition is true. If the condition is false, that block is simply skipped.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition) {
  // code that runs only when condition is true
}
</code></pre>
<h3>How it works — step by step</h3>
<pre><code class="language-plaintext">1. Evaluate the condition inside ()
2. If condition is TRUE  → enter the {} block and run the code
3. If condition is FALSE → skip the {} block entirely
4. Continue with the rest of the program
</code></pre>
<h3>Example — checking age</h3>
<pre><code class="language-javascript">const age = 20;

if (age &gt;= 18) {
  console.log("You are an adult.");
}

console.log("Program continues here.");

// Output:
// You are an adult.
// Program continues here.
</code></pre>
<p>Now with a condition that is false:</p>
<pre><code class="language-javascript">const age = 15;

if (age &gt;= 18) {
  console.log("You are an adult."); // ← skipped entirely
}

console.log("Program continues here.");

// Output:
// Program continues here.
</code></pre>
<p>The <code>if</code> block was completely skipped because <code>15 &gt;= 18</code> is false.</p>
<h3>More examples</h3>
<pre><code class="language-javascript">const marks = 75;

if (marks &gt;= 50) {
  console.log("You passed the exam!");
}

// ─────────────────────────────────────────
const temperature = 38;

if (temperature &gt; 37) {
  console.log("You have a fever. Please rest.");
}

// ─────────────────────────────────────────
const isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome back!");
}
</code></pre>
<blockquote>
<p>🔑 <strong>Key point:</strong> The condition inside <code>()</code> always evaluates to either <code>true</code> or <code>false</code>. JavaScript will automatically convert values if needed — <code>0</code>, <code>""</code>, <code>null</code>, <code>undefined</code>, and <code>NaN</code> are all treated as <code>false</code>. Everything else is treated as <code>true</code>.</p>
</blockquote>
<hr />
<h2>3. The <code>if-else</code> Statement</h2>
<p>The <code>if</code> alone only does something when the condition is true. The <code>if-else</code> adds a second path — something to do when the condition is <strong>false</strong>.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition) {
  // runs when condition is TRUE
} else {
  // runs when condition is FALSE
}
</code></pre>
<p><strong>One of these two blocks always runs — never both, never neither.</strong></p>
<h3>Example — pass or fail</h3>
<pre><code class="language-javascript">const marks = 43;

if (marks &gt;= 50) {
  console.log("Result: Pass ✓");
} else {
  console.log("Result: Fail ✗");
}

// Output: Result: Fail ✗
</code></pre>
<h3>Step-by-step execution</h3>
<pre><code class="language-plaintext">marks = 43

Step 1: Check → is 43 &gt;= 50?
Step 2: 43 &gt;= 50 is FALSE
Step 3: Skip the if block
Step 4: Enter the else block
Step 5: Print "Result: Fail ✗"
</code></pre>
<h3>More examples</h3>
<pre><code class="language-javascript">// Even or odd
const number = 7;

if (number % 2 === 0) {
  console.log(number + " is Even");
} else {
  console.log(number + " is Odd");
}
// Output: 7 is Odd

// ─────────────────────────────────────────
// Login check
const password = "abc123";
const correctPassword = "mySecret";

if (password === correctPassword) {
  console.log("Access granted.");
} else {
  console.log("Wrong password. Try again.");
}
// Output: Wrong password. Try again.

// ─────────────────────────────────────────
// Temperature check
const temp = 22;

if (temp &gt; 30) {
  console.log("It is hot outside.");
} else {
  console.log("The weather is comfortable.");
}
// Output: The weather is comfortable.
</code></pre>
<blockquote>
<p>💡 <strong>Try this in your console:</strong></p>
<pre><code class="language-javascript">const score = 85;
if (score &gt;= 90) {
  console.log("Grade: A");
} else {
  console.log("Grade: B or below");
}
// Change the score value and run again
</code></pre>
</blockquote>
<hr />
<h2>4. The <code>else if</code> Ladder</h2>
<p>Sometimes you have <strong>more than two possible outcomes</strong>. The <code>else if</code> ladder lets you check multiple conditions in sequence, handling each case separately.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition1) {
  // runs if condition1 is true
} else if (condition2) {
  // runs if condition1 is false AND condition2 is true
} else if (condition3) {
  // runs if condition1 and condition2 are false AND condition3 is true
} else {
  // runs if ALL conditions above are false
}
</code></pre>
<p>JavaScript checks conditions <strong>from top to bottom</strong>. It stops at the first one that is true and runs that block. The <code>else</code> at the end is the fallback — it only runs if nothing matched.</p>
<h3>Example — grading system</h3>
<pre><code class="language-javascript">const marks = 72;

if (marks &gt;= 90) {
  console.log("Grade: A");
} else if (marks &gt;= 75) {
  console.log("Grade: B");
} else if (marks &gt;= 60) {
  console.log("Grade: C");
} else if (marks &gt;= 50) {
  console.log("Grade: D");
} else {
  console.log("Grade: F — Please retake the exam");
}

// Output: Grade: C
</code></pre>
<h3>Step-by-step execution for marks = 72</h3>
<pre><code class="language-plaintext">Step 1: Is 72 &gt;= 90? → NO  → skip
Step 2: Is 72 &gt;= 75? → NO  → skip
Step 3: Is 72 &gt;= 60? → YES → run this block → print "Grade: C"
Step 4: Skip all remaining else-if and else blocks
Step 5: Continue with the rest of the program
</code></pre>
<p>Once a match is found, JavaScript <strong>exits the entire if-else ladder</strong> immediately. Conditions below the match are never checked.</p>
<h3>Example — age categories</h3>
<pre><code class="language-javascript">const age = 45;

if (age &lt; 13) {
  console.log("Child");
} else if (age &lt; 18) {
  console.log("Teenager");
} else if (age &lt; 60) {
  console.log("Adult");
} else {
  console.log("Senior");
}

// Output: Adult
</code></pre>
<h3>Example — traffic light</h3>
<pre><code class="language-javascript">const light = "yellow";

if (light === "green") {
  console.log("Go!");
} else if (light === "yellow") {
  console.log("Slow down.");
} else if (light === "red") {
  console.log("Stop!");
} else {
  console.log("Unknown signal — proceed with caution.");
}

// Output: Slow down.
</code></pre>
<blockquote>
<p>🔑 <strong>Order matters.</strong> In the grading example, if you put <code>marks &gt;= 50</code> first, a student with 95 marks would get Grade: D instead of Grade: A — because <code>95 &gt;= 50</code> is true and the ladder stops there. Always put the <strong>most specific condition first</strong>.</p>
</blockquote>
<hr />
<h2>5. The <code>switch</code> Statement</h2>
<p>The <code>switch</code> statement is designed for situations where <strong>one variable is compared to many specific values</strong>. Instead of writing <code>else if</code> after <code>else if</code>, you list out cases cleanly.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">switch (expression) {
  case value1:
    // code for value1
    break;

  case value2:
    // code for value2
    break;

  case value3:
    // code for value3
    break;

  default:
    // code if no case matched
}
</code></pre>
<h3>How it works — step by step</h3>
<pre><code class="language-plaintext">1. Evaluate the expression once
2. Compare the result against each case value
3. Jump to the matching case and run its code
4. hit break → exit the switch block entirely
5. If no case matched → run the default block
</code></pre>
<h3>Example — day of the week</h3>
<pre><code class="language-javascript">const 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;
  case 5:
    console.log("Friday");
    break;
  case 6:
    console.log("Saturday");
    break;
  case 7:
    console.log("Sunday");
    break;
  default:
    console.log("Invalid day number. Enter 1 to 7.");
}

// Output: Wednesday
</code></pre>
<h3>What does <code>break</code> actually do?</h3>
<p><code>break</code> is essential in <code>switch</code>. Without it, JavaScript does not stop at the matched case — it <strong>falls through</strong> and runs every case below it until it hits a <code>break</code> or the end of the switch.</p>
<pre><code class="language-javascript">const day = 2;

switch (day) {
  case 1:
    console.log("Monday");
    // no break!
  case 2:
    console.log("Tuesday");
    // no break!
  case 3:
    console.log("Wednesday");
    break;
  case 4:
    console.log("Thursday");
    break;
}

// Output:
// Tuesday    ← matched here
// Wednesday  ← fell through because case 2 had no break!
</code></pre>
<p>This fall-through behaviour is almost always a bug. <strong>Always add</strong> <code>break</code> <strong>at the end of each case</strong> unless you have a specific reason not to.</p>
<h3>The <code>default</code> case</h3>
<p><code>default</code> is like the <code>else</code> in an if-else ladder — it runs when no case matches:</p>
<pre><code class="language-javascript">const colour = "purple";

switch (colour) {
  case "red":
    console.log("Stop");
    break;
  case "green":
    console.log("Go");
    break;
  case "yellow":
    console.log("Slow down");
    break;
  default:
    console.log("Unknown colour: " + colour);
}

// Output: Unknown colour: purple
</code></pre>
<h3>Grouping cases — same result for multiple values</h3>
<p>You can stack cases together when they should produce the same output:</p>
<pre><code class="language-javascript">const day = "Saturday";

switch (day) {
  case "Monday":
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
  case "Friday":
    console.log("Weekday — time to work!");
    break;

  case "Saturday":
  case "Sunday":
    console.log("Weekend — time to rest!");
    break;

  default:
    console.log("Not a valid day.");
}

// Output: Weekend — time to rest!
</code></pre>
<blockquote>
<p>💡 <strong>Try this in your console:</strong></p>
<pre><code class="language-javascript">const month = 2;
switch (month) {
  case 1: case 3: case 5: case 7:
  case 8: case 10: case 12:
    console.log("31 days"); break;
  case 4: case 6: case 9: case 11:
    console.log("30 days"); break;
  case 2:
    console.log("28 or 29 days"); break;
  default:
    console.log("Invalid month");
}
</code></pre>
</blockquote>
<hr />
<h2>6. When to Use switch vs if-else</h2>
<p>Both <code>switch</code> and <code>if-else</code> make decisions — but they are suited to different situations.</p>
<h3>Use <code>if-else</code> when</h3>
<pre><code class="language-javascript">// ✅ Checking ranges or comparisons (&gt;, &lt;, &gt;=, &lt;=)
if (marks &gt;= 90) { ... }
else if (marks &gt;= 75) { ... }

// ✅ Checking complex or multiple conditions
if (age &gt;= 18 &amp;&amp; hasID === true) { ... }

// ✅ Conditions involve different variables
if (temp &gt; 35 || humidity &gt; 80) { ... }

// ✅ You only have 2–3 conditions
if (isLoggedIn) { ... } else { ... }
</code></pre>
<h3>Use <code>switch</code> when</h3>
<pre><code class="language-javascript">// ✅ One variable compared to many specific values
switch (day) { case 1: ... case 2: ... }

// ✅ Menu options or command handling
switch (userChoice) {
  case "start":  startGame();  break;
  case "pause":  pauseGame();  break;
  case "quit":   quitGame();   break;
}

// ✅ Status codes or fixed categories
switch (statusCode) {
  case 200: console.log("OK");           break;
  case 404: console.log("Not Found");    break;
  case 500: console.log("Server Error"); break;
}
</code></pre>
<h3>Side-by-side comparison</h3>
<table>
<thead>
<tr>
<th></th>
<th><code>if-else</code></th>
<th><code>switch</code></th>
</tr>
</thead>
<tbody><tr>
<td><strong>Checks</strong></td>
<td>Any condition (ranges, logic)</td>
<td>Exact value matches only</td>
</tr>
<tr>
<td><strong>Condition type</strong></td>
<td><code>&gt;</code>, <code>&lt;</code>, <code>&gt;=</code>, <code>&amp;&amp;</code>, `</td>
<td></td>
</tr>
<tr>
<td><strong>Multiple variables</strong></td>
<td>✅ Yes</td>
<td>❌ One expression only</td>
</tr>
<tr>
<td><strong>Readability</strong></td>
<td>Better for 2–3 conditions</td>
<td>Better for 5+ fixed values</td>
</tr>
<tr>
<td><strong>Fall-through</strong></td>
<td>❌ Not possible</td>
<td>✅ Possible (needs <code>break</code>)</td>
</tr>
<tr>
<td><strong>Default fallback</strong></td>
<td><code>else</code></td>
<td><code>default</code></td>
</tr>
</tbody></table>
<h3>The quick rule</h3>
<blockquote>
<p><strong>Range or complex logic →</strong> <code>if-else</code> <strong>Many exact values of one variable →</strong> <code>switch</code></p>
</blockquote>
<hr />
<h2>Diagrams</h2>
<h3>if-else Decision Flowchart</h3>
<pre><code class="language-plaintext">┌─────────────────────────────────────────────────────────────┐
│               IF-ELSE DECISION FLOWCHART                    │
│                                                             │
│                    ┌────────────┐                           │
│                    │   Start    │                           │
│                    └─────┬──────┘                           │
│                          ↓                                  │
│                 ┌────────────────┐                          │
│                 │  Condition     │                          │
│                 │  marks &gt;= 50?  │                          │
│                 └───┬────────┬───┘                          │
│                     │        │                              │
│                   TRUE      FALSE                           │
│                     ↓        ↓                              │
│             ┌────────────┐  ┌────────────┐                  │
│             │   "Pass"   │  │   "Fail"   │                  │
│             └─────┬──────┘  └─────┬──────┘                  │
│                   └────────┬───────┘                        │
│                            ↓                               │
│                    ┌────────────┐                           │
│                    │  Continue  │                           │
│                    └────────────┘                           │
│                                                             │
│  else-if ladder (multiple conditions):                      │
│                                                             │
│           ┌──────────────┐                                  │
│           │ marks &gt;= 90? ├── YES → "Grade A"                │
│           └──────┬───────┘                                  │
│                  │ NO                                        │
│           ┌──────┴───────┐                                  │
│           │ marks &gt;= 75? ├── YES → "Grade B"                │
│           └──────┬───────┘                                  │
│                  │ NO                                        │
│           ┌──────┴───────┐                                  │
│           │ marks &gt;= 60? ├── YES → "Grade C"                │
│           └──────┬───────┘                                  │
│                  │ NO                                        │
│           ┌──────┴───────┐                                  │
│           │    else      │──────→ "Grade F"                 │
│           └──────────────┘                                  │
│                                                             │
│  ✦ Checks top to bottom — stops at first TRUE condition     │
│  ✦ The else block only runs if nothing above matched        │
└─────────────────────────────────────────────────────────────┘
</code></pre>
<hr />
<h3>switch-case Branching Diagram</h3>
<pre><code class="language-plaintext">┌─────────────────────────────────────────────────────────────┐
│             SWITCH-CASE BRANCHING DIAGRAM                   │
│                                                             │
│   switch (day)                                              │
│         │                                                   │
│         ├── case 1  → "Monday"    → break ──┐              │
│         │                                   │              │
│         ├── case 2  → "Tuesday"   → break ──┤              │
│         │                                   │              │
│         ├── case 3  → "Wednesday" → break ──┤              │
│         │                                   │              │
│         ├── case 4  → "Thursday"  → break ──┤              │
│         │                                   │              │
│         ├── case 5  → "Friday"    → break ──┤              │
│         │                                   │              │
│         ├── case 6  → "Saturday"  → break ──┤              │
│         │                                   │              │
│         ├── case 7  → "Sunday"    → break ──┤              │
│         │                                   │              │
│         └── default → "Invalid"   ──────────┘              │
│                                   │                         │
│                              EXIT switch                    │
│                                                             │
│  What break does:                                           │
│  ─────────────────────────────────────────                  │
│  WITH break:    case 3 matches → runs "Wednesday"           │
│                 → break → exits immediately ✓               │
│                                                             │
│  WITHOUT break: case 3 matches → runs "Wednesday"           │
│                 → falls into case 4 → "Thursday"            │
│                 → falls into case 5 → "Friday"  ← bug!     │
│                                                             │
│  ✦ Always add break unless you intentionally want fallthrough│
└─────────────────────────────────────────────────────────────┘
</code></pre>
<hr />
<h2>Practice Assignment</h2>
<p>Try these three exercises in your browser console or in a <code>.js</code> file.</p>
<hr />
<h3>Task 1 — Positive, Negative, or Zero</h3>
<p>Write a program that takes a number and tells you whether it is positive, negative, or zero.</p>
<pre><code class="language-javascript">const number = -7; // change this value and test

if (number &gt; 0) {
  console.log(number + " is Positive");
} else if (number &lt; 0) {
  console.log(number + " is Negative");
} else {
  console.log("The number is Zero");
}

// Test with: -7 → "Negative"
// Test with:  0 → "Zero"
// Test with: 15 → "Positive"
</code></pre>
<p><strong>Why</strong> <code>if-else</code> <strong>here?</strong> Because we are checking <strong>ranges and comparisons</strong> (<code>&gt; 0</code>, <code>&lt; 0</code>). These are conditions that <code>switch</code> cannot handle — it only checks exact equality.</p>
<hr />
<h3>Task 2 — Day of the Week using switch</h3>
<p>Write a program that prints the name of the day based on a number (1 = Monday, 7 = Sunday).</p>
<pre><code class="language-javascript">const dayNumber = 5; // change this value and test

switch (dayNumber) {
  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;
  case 6:
    console.log("Saturday");
    break;
  case 7:
    console.log("Sunday");
    break;
  default:
    console.log("Invalid! Please enter a number between 1 and 7.");
}

// Test with: 5 → "Friday"
// Test with: 7 → "Sunday"
// Test with: 9 → "Invalid!"
</code></pre>
<p><strong>Why</strong> <code>switch</code> <strong>here?</strong> Because we are comparing <strong>one variable (</strong><code>dayNumber</code><strong>) against many exact values</strong> (1 through 7). This is exactly what <code>switch</code> is designed for — it is more readable than writing seven <code>else if</code> blocks.</p>
<hr />
<h3>Task 3 — Bonus: Grade Calculator</h3>
<p>Combine both — use an <code>if-else</code> ladder and explain your reasoning.</p>
<pre><code class="language-javascript">const marks = 68;

if (marks &gt;= 90) {
  console.log("Grade: A — Excellent!");
} else if (marks &gt;= 75) {
  console.log("Grade: B — Good job!");
} else if (marks &gt;= 60) {
  console.log("Grade: C — Satisfactory");
} else if (marks &gt;= 50) {
  console.log("Grade: D — Needs improvement");
} else {
  console.log("Grade: F — Please retake the exam");
}

// Output: Grade: C — Satisfactory
</code></pre>
<p><strong>Why</strong> <code>if-else</code> <strong>and not</strong> <code>switch</code> <strong>here?</strong> Because grades depend on <strong>ranges</strong> (<code>&gt;= 90</code>, <code>&gt;= 75</code>), not exact values. <code>switch</code> can only check if <code>marks === 90</code> or <code>marks === 75</code>, not whether marks fall <em>within a range</em>. For ranges, <code>if-else</code> is always the right choice.</p>
<hr />
<h2>Quick Reference</h2>
<pre><code class="language-javascript">// ─── if ───────────────────────────────────────────────────
if (condition) {
  // runs only when condition is true
}

// ─── if-else ──────────────────────────────────────────────
if (condition) {
  // true path
} else {
  // false path
}

// ─── else-if ladder ───────────────────────────────────────
if (condition1) {
  // first match
} else if (condition2) {
  // second match
} else if (condition3) {
  // third match
} else {
  // no match — fallback
}

// ─── switch ───────────────────────────────────────────────
switch (value) {
  case "a":
    // code for "a"
    break;
  case "b":
    // code for "b"
    break;
  default:
    // no match fallback
}

// ─── Grouped cases ────────────────────────────────────────
switch (value) {
  case "x":
  case "y":
    // runs for both "x" and "y"
    break;
}
</code></pre>
<hr />
<h2>Wrapping Up</h2>
<p>Control flow is what makes programs intelligent. Without it, your code does the same thing every single time — with it, your program can respond to data, user input, and real-world conditions.</p>
<p>Here is what you learned:</p>
<ul>
<li><p><strong>Control flow</strong> means your program can choose different paths based on conditions</p>
</li>
<li><p><code>if</code> runs a block only when a condition is true</p>
</li>
<li><p><code>if-else</code> gives you two paths — one for true, one for false</p>
</li>
<li><p><code>else if</code> <strong>ladder</strong> handles multiple conditions in sequence, top to bottom, first match wins</p>
</li>
<li><p><code>switch</code> cleanly handles one variable compared against many exact values — always use <code>break</code></p>
</li>
<li><p><strong>Use</strong> <code>if-else</code> for ranges and complex logic; <strong>use</strong> <code>switch</code> for many exact values of one variable</p>
</li>
</ul>
<p>Start with the assignment exercises above, change the values and re-run them, and you will be comfortable with all of these in no time.</p>
<p>Happy coding! 🚀</p>
<hr />
<p><em>The fastest way to learn control flow: pick a real decision from your daily life (what to wear, what to eat) and try to write it as an if-else ladder in JavaScript.</em></p>
]]></content:encoded></item></channel></rss>