What a program is and what the computer does with it

Video lessonComing soon

1 A program is a list of steps

A program is a list of instructions. The computer reads them one at a time, from top to bottom, and does exactly what each one says.

It does not guess what we meant. If a step is wrong, the computer still follows it, and the program gives a wrong answer. So the whole job of programming is to write steps that are correct for every input, not just the one we tried.

A recipe is a good picture. "Boil water, add rice, wait 15 minutes" works because each step is clear and they come in the right order. Swap the last two steps and the dish fails, even though every step is still a valid step.

2 What the computer remembers: variables

While a program runs, it keeps values in memory. A variable is a name for one place in memory. We can picture it as a labelled box that holds one value at a time.

When we write x = 5, the computer puts 5 in the box called x. When we later write x = 10, the 10 replaces the 5. The old value is gone.

Here are three steps, and what each box holds after each step:

x = 5
y = x + 3
x = y * 2
After stepxy
x = 55—
y = x + 358
x = y * 2168
5x—y
after x = 5
5x8y
after y = x + 3
16x8y
after x = y * 2

Notice that y = x + 3 reads x at that moment. Changing x later does not change y. A table like this one is called a trace table, and we use it a lot in the lesson on dry runs.

3 Input, work, output

Almost every program does three things:

  1. It takes input: the values it works on.
  2. It does some work on them: the steps.
  3. It gives back output: the answer.

On oneby7, and in most coding interviews, we write a function. Its parameters are the input and its return value is the output. Reading the input and printing the answer is already done for us, so we only write the work.

Here is a complete answer to the problem "return the sum of two numbers":

The shape is the same in every language. The function gets a and b, makes a variable sum, and returns it. Only the spelling changes.

4 From code to a running program

The code we write is plain text. The computer cannot run text directly, so a tool first turns it into something the machine understands. There are two kinds of tool.

  • A compiler reads the whole program first and translates it before anything runs. C++, Go and Rust work this way. Java and C# also compile first, into an in-between form that a virtual machine then runs.
  • An interpreter reads the program and runs it as it goes. Python and JavaScript work this way.

The difference matters in two places. A compiled language finds some mistakes before the program starts, such as a missing bracket or a wrong type. A compiled program also usually runs faster. For the problems in this course, any of the seven languages is fast enough when the method is right.

5 Four ways a program goes wrong

When we submit code, it gets one of these results. Each one points to a different kind of mistake.

VerdictWhat happenedUsual cause
Compile ErrorThe code breaks the language's rules, so it never runsA typo, a missing ; or }, a wrong type
Runtime ErrorThe program started, then crashedReading past the end of an array, dividing by zero
Wrong AnswerIt ran and finished, but the answer is wrongA mistake in the steps, or a case we did not think of
Time Limit Exceeded (TLE)It is correct but too slowDoing far more work than the problem allows

Accepted means the code passed every test. A Compile Error is the easiest to fix, because the message names the line. A Wrong Answer is the hardest, because nothing points at the mistake. The lesson on dry runs is about finding those.

6 How fast is a computer?

A computer is fast, but not endlessly fast. A good rule of thumb is that it does about 10⁸ simple steps in one second. A simple step is something like an addition, a comparison or reading one array value.

That one number tells us a lot before we write any code:

  • A loop over 10⁵ values does around 10⁵ steps. That finishes almost at once.
  • Two nested loops over 10⁵ values do around 10¹⁰ steps. At 10⁸ a second that is about 100 seconds, far past a time limit of one or two seconds, so it gets TLE.

So the same correct idea can pass or fail depending on how much work it does. Counting that work is what Big-O is for, and it gets its own lessons in this topic.

The whole lesson in one line: a program is a list of exact steps, run one at a time, working on values kept in memory.

Finished this lesson?

Mark it read and it is ticked on the roadmap and in the list.

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.