Conditions, Loops and Events
Lesson preview: the Mini Lesson and Vocabulary are open to everyone. Guided practice, exercises, the parent checklist and the printable PDF worksheet come with full access (free 7-day trial or membership).
Learning Objectives
- 1Define conditions, loops, and events as the three control patterns of code
- 2Identify each pattern from a sample code block or pseudocode line
- 3Match a code block to the behavior it produces in a program
- 4Combine a condition, a loop, and an event in a small program plan
Mini Lesson
A program is a list of instructions, but a program that only runs from top to bottom is rare. Real code makes choices, repeats steps, and reacts to the outside world. Three patterns power almost every program you will ever write: conditions, loops, and events.
The Three Control Patterns
- Condition — code that picks a path based on a yes / no test (if, else, switch).
- Loop — code that repeats the same action many times (for, while, repeat).
- Event — code that waits and reacts when something happens (on click, on key press, on message).
Conditions — choosing a path
- A condition is a yes / no test such as score > 10 or temperature < 0.
- if runs the block when the test is true.
- else runs a different block when the test is false.
- You can chain tests with else if to choose between many paths.
- Without conditions, every program would do exactly the same thing every time it runs.
Loops — repeating an action
- A for loop repeats a fixed number of times — for example, repeat 5 times.
- A while loop repeats while a condition stays true — for example, while alive.
- An until loop repeats until a condition becomes true — for example, until full.
- Loops save typing — instead of writing the same line twenty times, you write it once inside a loop.
- Every loop needs a way out, or the program freezes — this is called an infinite loop.
Events — reacting to the world
- An event is a signal from outside the program: a click, a key press, a timer, a message.
- on click runs a block when the user clicks the mouse.
- on key pressed runs a block when a key is pressed on the keyboard.
- on start runs a block when the program first begins.
- broadcast lets one part of the code send a signal to other parts.
Tip: when you plan a program, ask three questions. What choice does it make? (condition) What does it repeat? (loop) What does it react to? (event) Naming the three patterns up front makes the code much easier to write.
Unlock the full HSEA system for 7 days.
Add your first kid profile and your 7 days start: every lesson in every grade, saved progress for each child. Nothing to set up, no card required, nothing to cancel.
- ✓ All 1,680 lessons, Grades 1–6, every subject
- ✓ Profiles + saved progress for every kid
- ✓ Printable lessons with answer key