lesson

Updated 6 days ago ยท 2 views
Imagine you have a toy box labeled score in your favorite video game. Every time you collect a shiny gold coin, the number inside that box changes!
In programming, a variable is a labeled box that holds a value, and a sequence is a list of code instructions that run in order from top to bottom.
๐Interactive diagram
How do you know what number is inside the box after every single instruction runs?
Tracing Step-by-Step
Tracing means reading code line by line and writing down the value inside each variable after each step.
๐Interactive diagram
On line 2, the computer looks inside the box, finds the number 4, adds 3 to it, and puts the new total of 7 right back in.
What happens if a new line of code gives the box a brand-new number instead of adding to it?
Overwriting vs. Adding
When code gives a variable a new number without using a plus sign, it overwrites (replaces) whatever was inside before.
๐Interactive diagram
If
stars = 10 is followed by stars = 2, the box holds 2, not 12.