Control Structures
Control Structures — CS 111 Review.
Control Structures
Control structures decide which lines of code actually run, when they run, and how many times. They're the difference between a flat script and a program that reacts to whatever is happening in the moment — without them, code just plays out top-to-bottom with nothing branching, repeating, or responding to input.
Execution Flow
Click any block to read about it. The chain it depends on will light up.
Execution
Control Flow
Loop
If / Else
Nested If
Pick a block above to see how it shapes flow.
The Three Categories
- Iteration (Loops). A loop runs the same block of code over and over while a condition stays true.
for (let i = 0; i < 5; i++)repeats five times. In a game, loops are how you update every sprite on screen each frame or sweep through a list of enemies checking for collisions. - Conditions (If / Else). A condition checks whether something is true and picks which branch of code to run.
if (health <= 0) { die(); }only runs if the player is out of health. Games rely on conditions for game-over screens, keyboard input, damage handling, and almost every reactive moment. - Nested Conditions. A nested condition is a check inside another check — useful when one decision only matters after a previous one has gone a certain way. AI uses this constantly: an enemy might first check whether it's alert, then check whether the player is within attack range.
if (enemy.isHostile) { if (distance < 50) { enemy.attack(); } }
The Three Structures — Tabbed View
Click a structure to switch tabs.
Iteration — Repeat
Loops let you write a block once and have the computer run it as many times as you need. Pick the loop type that matches how the repetition ends.
| Loop | Use when… |
|---|---|
for |
You know the count up front |
while |
You repeat until something changes |
for...of |
You’re walking through an array |
In games: updating sprites every frame, scanning collisions, running AI ticks, processing inventory.
Conditions — Decide
if / else is how your code reacts to what's actually happening right now.
if (health <= 0) { die(); }
else if (health < 25) { playWarning(); }
else { continue(); }
In games: state changes, input handling, damage, win/lose logic, AI reactions.
Nested Conditions — Refine
Putting one condition inside another lets you express layered logic — where the second check only matters if the first one passed.
if (enemy.isHostile) {
if (distance < 50) {
enemy.attack(); // only if BOTH are true
}
}
In games: AI decision trees, proximity checks, multi-state interactions.
Quick Reference
Structure
Keyword
Purpose
Iteration
for, while, for...ofRepeat actions
Condition
if, else if, elseBranch on truth
Nested Condition
if inside ifLayer decisions
Key Things to Remember
- If something needs to repeat, that's a loop.
- If something needs to choose, that's a condition.
- If a choice depends on another choice, nest them.
- Avoid stacking five layers deep — if your nesting gets that bad, the logic itself probably needs to be redesigned.