START
x = 0
x < 5?

The Logic of Code

Discover how computers make decisions with variables, conditions, loops, and functions

1

Variables: Boxes That Hold Values

A variable is like a labeled box that stores information

score
0
number
playerName
"Alex"
string
isAlive
true
boolean
let score = 0;
let playerName = "Alex";
let isAlive = true;

Why Variables Matter

Variables let programs remember information. A game tracks your score, a website remembers your username, and an app saves your preferences - all using variables!

2

Conditionals: Making Decisions

Computers use IF-THEN-ELSE to choose what to do

START
Check: score >= 100?
YES (true)
You Win!
NO (false)
Keep Playing
0
Current Score
Keep Playing
Result
if (score >= 100) {
console.log("You Win!");
} else {
console.log("Keep Playing");
}

Real World Example

Every login form uses conditionals: IF password is correct THEN let user in, ELSE show error message. Video games use thousands of these decisions every second!

3

Loops: Repeat Until Done

Why write the same code 100 times when you can loop once?

0
for (let i = 0; i < 10; i++) {
console.log("Iteration:", i);
}
// Runs 10 times!

Loops Are Everywhere

Drawing 60 frames per second in a game? Loop. Processing 1000 emails? Loop. Checking every pixel in an image? Loop. Computers are really just very fast loopers!

4

Functions: Reusable Recipes

Write code once, use it anywhere - like a recipe card

function calculateDamage(baseDamage, multiplier)
Input
10, 2
× multiply
Output
20
function calculateDamage(baseDamage, multiplier) {
return baseDamage * multiplier;
}
// Call the function:
let damage = calculateDamage(10, 2);
// damage = 20

Functions = Superpowers

Instead of writing damage calculation code every time an attack happens, you write it ONCE as a function. Game has 50 attack types? Just call calculateDamage() 50 different ways!

5

Boolean Logic: AND, OR, NOT

The building blocks of all computer decisions

AND
0
0
A AND B =
0
Both must be true
OR
0
0
A OR B =
0
At least one true
NOT
0
NOT A =
1
Flips the value
// Game example: Can player open the door?
let hasKey = true;
let doorUnlocked = false;
if (hasKey || doorUnlocked) { // OR
openDoor();
}

Logic Gates Power Everything

Your computer's CPU contains billions of tiny logic gates making AND, OR, NOT decisions. Every calculation, every game frame, every click - it's all gates working together at lightning speed!

6

Build Your Own Logic

Drag nodes to create a simple program flowchart

Nodes

🟢 Start
⬜ Process
🔷 Decision
🔴 End

Variable

score =
Drag nodes to build your flowchart

• Double-click nodes to edit
• Drag from ● to ● to connect
• Use decisions to check score
Build a flowchart and click Run to see it execute...
Double-click to edit • Drag between circles to connect • Decisions check if score > 50

Think Before You Code

Professional programmers often sketch flowcharts before writing code. It helps you see the logic clearly and catch problems early. Planning saves time!

You've Learned the Logic!

Now you understand the building blocks that power every program, game, and app.

0
Variables Changed
0
Loop Iterations
0
Logic Gates Toggled

Variables

Named boxes that store data - numbers, text, true/false

Conditionals

IF-THEN-ELSE lets programs make decisions

Loops

Repeat code without rewriting it - computers are fast loopers!

Functions

Reusable code recipes - write once, use everywhere

Ready to Create?

Put your new knowledge into practice!

Suggest a Correction