What is code, really?
Programming, coding. It looks hard and special, somehow. But what it really is is surprisingly simple. Telling a computer what to do, in order, that's all it is.
A recipe and directions are code too
What code is, in fact,
we already know.
Think of a recipe.
"Boil the water,
add the noodles,
wait three minutes."
A to-do list written in order.
This is 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
The code we use every day.
Instructions written in order,
telling someone what to do.
Code is the same.
It's just that the listener
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, in turn.
Like a person following a recipe
one step at a time.
Very fast,
but always in order.
Try pressing play.
It runs one line at a time.
It finishes one line
before moving to the next.
Order is the flow itself.
This simple rule
is the backbone of every program.
Change the order and the result changes
Why does "in order" matter?
Because changing the order
changes the result.
"Put on socks, then shoes"
versus "put on shoes, then socks."
The same actions,
just a different order,
yet completely different.
Same actions, different order.
A computer is the same.
Swap the order of "take the money"
and "give the goods,"
and you get a totally different result.
So when you write code,
you have to set the order carefully.
A computer does exactly as told
Tell a person "just tidy up a bit"
and they figure it out.
A computer doesn't.
It does only what it's told,
exactly as told.
There's no "sort of" or "use your judgment."
So if you leave something out or tell it wrong,
it does the wrong thing exactly.
What you said versus what it did.
This looks frustrating,
but it's actually a big advantage.
Because it does exactly as told,
the same code
always behaves the same.
That means it's reliable.
In return, we have to tell it precisely.
Tell a robot the way
Now it's your turn.
To send the robot to the flag,
how should you tell it?
Stack the commands in order,
then press run.
Get the order right and it reaches the flag,
otherwise it stops somewhere odd.
Stack the commands, then run.
What you just did
is programming itself.
Lining up commands in order,
and having the computer run them as is.
Next time we'll see
where the "values" these commands handle
are kept.