Maisy Dinosaur

Maisy Dinosaur

← Back to course

What is a program?

Learning the basics of how computers and humans communicate

This page works best on desktop or tablet

In this lesson, we'll cover how computers work and how they understand and follow the instructions we give them.

What is code?

Code is a set of instructions that a computer can understand, similar to a recipe. It's how humans communicate with computers. Computers look at this code and read through it line by line, following each instruction in order.

In the examples below, I'll use plain english sentences to make it clear what's going on, but in later lessons we will switch to a real programming language. The writing may look a little bit weird, but that's to keep each instruction consistent. Talking with computers is all about consistency! For example, when we want to refer to a piece of text, we'll make sure to surround it with quotes so that the computer knows when the text starts and when it stops.

Use the left and right arrows to move forward and backward through the steps, or click on a step number to skip to it. The arrow on the left will point to which line is currently running. When the computer says something, it will appear just below the code.

👉123

How do computers remember?

Just like a human, a computer has memory. You can ask the computer to remember things for you. Each piece of stored information is called a variable. Variables have two parts: a name and a value. Let's try having the computer remember something!

👉123

Notice how when the program continues to run, the variables don't disappear; this is both good and bad. It's good that we can use this data whenever we want; however, each variable we store takes up some amount of the computer's memory. Just like a human, computers have a limited amount of it, and when it runs out, it needs to make space before it can store new things. For now we'll ignore this since the program we're writing is so small that it doesn't even get close to using up all the memory in a modern computer.

Cool—we have some stuff stored in variables, but what are these good for? Well, you can use that data later! Let's try it now by telling the computer to remember something, and then asking it to repeat it back to us.

👉123

Look at that! But words aren't all the computers can store; computers can store all kinds of things. In computer science, we call these different things data types. For example:

Numbers

👉123

Booleans (true or false)

👉123

Lists

👉123

There are more data types too, but we won't get into those right now.

So now we have some information stored, but what happens when we want to change that information?

One option is just giving the computer a new value to store in that variable. Note that when you change the value in a variable, the old value is overwritten.

👉123

The variable changed! But, what happens when we want to modify the value instead of just overwriting it? We can also tell the computer to do that.

👉1234567

With this, we can start having the computer do things for us, like simple math!

👉123

The computer can do that much faster than a human could (or at least faster than I could).

How do computers make choices?

We now know how to store and retrieve data, but we can't do much if the computer can only ever run the same set of instructions. That's where control flow statements come in. Control flow statements let us tell the computer when (and when not) to run some set of instructions.

One of the most basic types of control flow statements is the if statement. This tells the computer to only run some piece of code if the condition we give it is met.

👉1234567

Notice how the computer skipped over line 6, since our number wasn't lower than 4. We can use this to have the computer run different lines of code depending on what its current state is. Now we can tell the computer to make decisions on its own!

But this still doesn't quite cover everything we might want to do,. Let's take a look at another type of control flow statement: the while loop! The while loop tells the computer to keep running a piece of code as long as its condition is met.

👉123456

With this we can run code for as many times as needed (even forever). Let's try putting this together with our variables to make the computer do something simple, like multiplication.

👉12345678

Here we achieved multiplication using just the small set of things given to us. Since we didn't have a way to multiply, we made it ourselves by repeatedly adding the second number to our total. This is something that happens all the time in programming! Ok, maybe not remaking multiplication, but programming is all about solving problems one by one using the tools you have. Plus, one of the coolest parts about programming is that if you want a tool that you don't have, you can just build it!

Review

That's all we'll go over in this lesson, but there's still lots more to learn! Let's review what we've learned so far:

  • Code is like a recipe for a computer
  • Code is evaluated line by line
  • We can have the computer store data in variables
  • We can modify those variables later
  • We can tell the computer to only evaluate certain code if a condition is met using an if statement
  • We can have the computer run code multiple times using a while loop
  • We can put this together to build lots of things!