Logic Brain Gym: Mastering Control Flow

This is where JavaScript stops being just a language and starts becoming a way of thinking.
β€œAt this stage, you either level up… or give up. We level up.”

πŸ”€ Conditions: if, else if, else

Logic in JavaScript starts with one simple question:

If this is true, what should happen? If not, then what?

JavaScript

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

Real-life example:

JavaScript

let age = 18;

if (age >= 17) {
  console.log("You may enter the cinema 🎬");
} else {
  console.log("Sorry, not old enough πŸ˜…");
}
          

JavaScript has zero empathy. True runs. False gets skipped.

βš–οΈ Comparison Operators & Booleans

Operator Meaning
==Equal value (not recommended)
===Equal value & type (USE THIS)
!=Not equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal
JavaScript

console.log(5 === "5"); // false
console.log(5 === 5);   // true
          

Boolean values are brutally simple: true or false. No middle ground.

πŸ”— Logical Operators

Operator Meaning
&&AND
||OR
!NOT (reverse)
JavaScript

let age = 20;
let hasTicket = true;

if (age >= 17 && hasTicket) {
  console.log("Welcome to the cinema 🍿");
}
          

🧭 Switch Case

When conditions get messy, switch keeps things readable.

JavaScript

let day = "Monday";

switch (day) {
  case "Monday":
    console.log("Time to work πŸ’Ό");
    break;
  case "Sunday":
    console.log("Rest day 😴");
    break;
  default:
    console.log("Just another day");
}
          

Forget break? Everything below it runs. Chaos mode activated.

πŸ” Loops: Repeat Without Crying

for loop

JavaScript

for (let i = 1; i <= 5; i++) {
  console.log("Count:", i);
}
            

while loop

JavaScript

let count = 1;

while (count <= 3) {
  console.log(count);
  count++;
}
          

do while loop

JavaScript

let num = 1;

do {
  console.log(num);
  num++;
} while (num <= 3);
          

πŸ§ͺ Practice Time

Odd / Even Check

JavaScript

let number = 7;

if (number % 2 === 0) {
  console.log("Even");
} else {
  console.log("Odd");
}
          

Multiple Check

JavaScript

let num = 20;

if (num % 5 === 0) {
  console.log("Multiple of 5");
} else {
  console.log("Not a multiple of 5");
}
          

🎟️ Mini Program: Cinema Ticket

JavaScript

let age = 19;
let money = 50000;

if (age >= 17 && money >= 40000) {
  console.log("Ticket purchased 🎬");
} else {
  console.log("Requirements not met πŸ˜”");
}
          

This is real-world logic. Programming is decision-making, not math.

πŸ”₯ Mini Challenges

  • FizzBuzz (3, 5, 3 & 5)
  • Loop 1–10 β†’ Odd / Even
  • Login simulation (username & password)

Try first. Don’t peek. Your brain needs reps.

🧠 Key Takeaways

  • Logic is the heart of JavaScript
  • if–else is the foundation
  • Operators must be memorized
  • Loops save time and sanity
  • Mini programs train thinking, not typing
Logo SICODER