Skip to main content

Command Palette

Search for a command to run...

Match Day Decisions: Mastering Control Flow in JavaScript 🏟️

Updated
3 min readView as Markdown
Match Day Decisions: Mastering Control Flow in JavaScript 🏟️

In programming, Control Flow is the order in which individual statements are executed. Normally, code runs from top to bottom, like a referee’s pre-match checklist. But sometimes, we need to take a detour based on what's happening on the pitch.

1. The if Statement: The Referee’s Call

The if statement is a simple "Check." If the condition is true, the code inside the block runs. If not, it’s ignored.

Scenario: Checking if a player deserves a Yellow Card.

let foulSeverity = "medium";

if (foulSeverity === "medium") {
    console.log("Referee shows a Yellow Card! 🟨");
}

2. The if-else Statement: Two Outcomes

Sometimes there are only two possibilities: Win or Lose, In or Out.

Scenario: Is the ball in the net?

let ballCrossedLine = true;

if (ballCrossedLine) {
    console.log("GOAL! ⚽");
} else {
    console.log("Play continues...");
}

3. The else if Ladder: Multi-Decision Tactics

In a real match, things aren't always black and white. You might have multiple conditions to check.

Scenario: Evaluating a player's stamina.

let stamina = 40;

if (stamina > 80) {
    console.log("Player is Sprinting! 🔥");
} else if (stamina > 30) {
    console.log("Player is Jogging. 🏃");
} else {
    console.log("Player needs a Substitution! 🪑");
}

4. The switch Statement: The League Table

When you have one variable that could be many specific things, the else if ladder gets messy. That's when we use switch.

Scenario: Printing the result based on the match outcome code.

let matchResult = "W"; 

switch (matchResult) {
    case "W":
        console.log("3 Points gained! 🏆");
        break; // Crucial! This stops the code from checking other cases.
    case "D":
        console.log("1 Point gained. 🤝");
        break;
    case "L":
        console.log("0 Points. Better luck next time. ❌");
        break;
    default:
        console.log("Result pending...");
}

Pro Tip: Always remember the break keyword. Without it, your code will "fall through" and execute every case after the match, which is like giving a team a Win, a Draw, and a Loss all at once!


5. Switch vs. If-Else: When to use which?

Use if-else when...

Use switch when...

You are checking ranges (e.g., score > 90).

You have fixed values (e.g., day === "Monday").

You have complex logic with && or `

You only have 1 or 2 conditions.

You want cleaner, more readable code for 4+ specific options.


Day 3 Assignment: The League Reporter 📝

Open your console and write these two programs:

  1. The Number Scout: Write an if-else if ladder that checks a number and prints if it is Positive, Negative, or Zero.

  2. The Calendar: Use a switch statement that takes a number (1-7) and prints the corresponding Day of the Week (e.g., 1 = Monday).

Which structure did you find easier to write? Tell me in the comments!