How Code Executes, Step by Step

You know what a program is, how its text becomes action, and where its values live. Time to put it together and watch execution happen in slow motion. This is the skill everything else in the academy builds on: being able to read code and correctly predict, step by step, what the machine will do.

Straight-line code

By default, execution flows top to bottom, one statement at a time. Each assignment updates memory, and the next line runs with that updated state. No skipping, no look-ahead.

Decisions: if and else

An if statement is a fork. The condition is checked at the moment execution reaches it, using the values in memory right then, and exactly one branch runs. Code in the branch not taken simply never happens.

Repetition: while

A while loop checks its condition, runs its body if the condition holds, then jumps back and checks again. The loop's body usually changes the very values the condition reads, so eventually the check fails and execution moves on. Loops are where programs spend nearly all of their time.

Step through this program with the controls below. Before each step, predict what will change, then check yourself against the variables table. Going back is allowed and encouraged.

Notice how the loop body ran three times, and how i had to climb past 3 to make the condition fail before the if got its turn. Off-by-one confusion around loop edges is the single most common programming mistake in the world. The challenge below lets you fix one.

What you just did, executing a program while tracking its state by hand, has a name in this field: tracing. It is exactly what a reverse engineer does to obfuscated code, except they trace through hostile territory with none of the names you enjoy here. And the stepping tool you just used is a tiny debugger. In Level 5 you will see the industrial versions attackers actually use.

Your first challenge

This program is supposed to print the numbers 1 through 5, one per line. It does not. Run it, read the output, find the bug, fix it. Hints are there if you want them.

One more, with no training wheels. This time the loop is yours to write from scratch.

That is Level 0 complete. You can read a program, predict its execution, and reason about its state, which quietly makes you more literate than most people who buy or sell code protection. Level 1 widens the view to seven real languages at once.

Frequently asked questions

What happens if I write a loop that never ends?

In this academy's playground, nothing bad: the interpreter has a hard step limit and stops any runaway program instantly with a clear message. On a real machine, an endless loop just spins until you kill the program, which is also a lesson worth having learned somewhere safe.

I finished the challenge. Am I ready for Level 1?

Yes. Level 1 takes the concepts you just used, variables, conditions, loops, and adds functions and scope, showing every one of them in eight languages side by side. Nothing there assumes more than what you did on this page.

Keep learning

  • Where Values Live: Memory, Stack, and Heap
  • Programming Fundamentals, One Idea in Seven Languages
  • What Is a Program?
  • Lua Obfuscator
  • JavaScript Obfuscator
  • Python Obfuscator

All lessons