Programming with Loops and Variables
Design and debug programs that use repetition, selection and variables: repeat actions with loops, branch with if-blocks, keep score in a variable, and break a bigger problem into smaller parts that can be programmed one at a time
What a learner can do afterwards
- Use a loop instead of repeating the same blocks (e.g., 'repeat 4' to draw a square)
- Use an if-block so the program responds to input (e.g., 'if the sprite touches the wall, bounce')
- Store and update a value in a variable, such as a score that goes up or a countdown timer
The lesson
A loop repeats the same steps for you. Instead of stacking blocks like 'move' and 'turn' over and over, you use one loop that says 'repeat 4' and put the steps inside it.
An if-block lets your program react instead of just repeating. It checks something first, like 'is the sprite touching the wall?' If that is true, the steps inside run. If not, they are skipped.
Every time the cat in a game catches a fish, the code adds 1 to a variable called score. The variable starts at 0, and after three catches it holds 3.
Big programs are easier to build in small pieces. Get the loop working first, then add the if-block, then add the variable. Test each piece before you add the next one.
Loops repeat steps, if-blocks react to what happens, and variables remember numbers that change, so build and test each piece one at a time.
Watch it
Where it sits
Learn first
This opens up
Nothing builds on it yet.
Where this leads
Jobs that lean on this skill. Follow one to see everything it is built on.
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.