👋 Welcome to the Decision Factory!
Computer programs are essentially giant decision-making machines. Conditionals are the instructions that tell the computer "If this happens, do that. Otherwise, do something else." Use the tabs above to explore the different ways computers think.
The "If" Statement
Think of an if statement like a Club Bouncer.
The bouncer checks your ID. IF you are old enough (True), you get in. ELSE (False), you stay outside.
- ✅ Condition: The question being asked (e.g., "Age >= 18?").
- 🏃 Action: What happens if the answer is Yes.
- 🛑 Else: The fallback plan if the answer is No.
if (age >= 18) {
console.log("Welcome to the party! 🎉");
} else {
console.log("Sorry, kiddo. 🍼");
}
🚪 The Digital Bouncer
Outcome Probability
Visualizing the binary nature of a simple IF/ELSE
Making Complex Decisions
Rarely is life as simple as one question. Usually, we need to check multiple things at once. We connect these checks using Logic Operators.
The AND Operator (&&)
Both conditions MUST be true.
"I want coffee AND a donut."
The OR Operator (||)
At least ONE condition must be true.
"I want coffee OR tea."
The NOT Operator (!)
Flips the value to its opposite.
"I do NOT want decaf."
The "Else If" Ladder 🪜
Great for rangesBest used when checking complex conditions or ranges (like grades: >90, >80, >70). It checks each step one by one.
if (fruit === 'apple') {
price = 1.00;
} else if (fruit === 'banana') {
price = 0.50;
} else if (fruit === 'cherry') {
price = 3.00;
} else {
price = 0;
}
The "Switch" Board 🎛️
Great for exact matchesBest used when comparing a single variable against many specific values (like menu items). It jumps directly to the matching case.
switch (fruit) {
case 'apple':
price = 1.00;
break;
case 'banana':
price = 0.50;
break;
default:
price = 0;
}
🤖 The Vending Machine Simulator
Select a snack code to see how a SWITCH statement processes it.
The Ternary Operator ⚡
Programmers love shortcuts. Why write 5 lines of code when you can write 1?
The Ternary operator is a one-line if/else.
The Long Way (If/Else)
let status;
if (isOnline) {
status = "Connected 🟢";
} else {
status = "Offline 🔴";
}
The Short Way (Ternary)
let status = isOnline ? "Connected 🟢" : "Offline 🔴"; // Condition ? IfTrue : IfFalse
Result: