What is code
Programming, coding: they sound hard and special. But what they really are is surprisingly simple: words that tell a computer what to do, in order. That's all.
Recipes and directions are code too
We actually already know what code is.
Think of the recipe on the back of an instant-noodle pack.
"Boil the water, add the noodles and the seasoning, wait 4 minutes 30 seconds."
A to-do list written in order: that's the essence of code.
Cooking ramen
- 1Boil 550ml of water
- 2Add the noodles and powder
- 3Wait 4 min 30 sec
- 4Turn off the heat
The way to a friend's place
- 1Go straight
- 2Turn left at the store
- 3Go 100 meters
- 4It's the second house
Code we use every day.
Instructions written in order to tell someone what to do: code is exactly that.
The only difference is that the one listening is a computer, not a person.
A computer reads from the top
A computer reads code from top to bottom, one line at a time.
Just as a person follows a recipe step by step, it goes very fast but always in order.
Press run below and the results appear one line at a time.
It runs one line at a time.
One line has to finish before the next one starts.
Order is the flow, and this simple rule is the backbone of every program.
Change the order and the result changes
Why does "in order" matter so much?
Because changing the order changes the result.
"Put on your socks, then your shoes" and "put on your shoes, then your socks" are the same actions, yet the order alone makes a completely different result.
Same actions, different order.
Computers are the same.
Swap the order of "take the money" and "hand over the item," and you get a totally different result.
That's why, when you write code, you have to choose the order carefully.
A computer takes you literally
Ask a person to "tidy up a bit" and they'll figure out what to do.
A computer won't. It does only what you said, exactly as you said it.
There's no "roughly" or "use your judgment," so if you leave something out or say it wrong, it'll go ahead and do the wrong thing.
What I said versus what it did.
That might seem frustrating, but it's actually a big strength.
Because it does exactly what it's told, the same code always behaves the same way, which means you can rely on it.
In return, we have to give precise instructions.
Give a robot directions
Now it's your turn.
How many "forward one square" commands do you need to stack to get the robot to the flag?
Stack the commands and press run: with the right number it reaches the flag, and with too few it stops partway.
Stack commands, then run.
What you just did is programming.
Lining up instructions in order and having the computer carry them out exactly.
In the next lesson, we'll see where the "values" these instructions work with are kept.