All topics
1

Instructions a computer follows exactly

Free

1. The problem

You are telling someone over the phone how to draw a simple house: a square with a triangle roof. They can't see your paper. They can't ask questions. Whatever you say, they draw. How do you make sure the drawing comes out right?

Every program is in the same position. The computer is the person on the other end of the phone.

2. Exact beats obvious

To a person, "draw a house" is enough. To a computer, those words mean nothing. It needs the small steps, in order:

  • draw a square, 4 cm on each side
  • on top of the square, draw a triangle the same width
  • done

The computer is fast, and it never gets tired. It takes every word as it is written, and it never fills a gap on its own.

VAGUE "draw a house" computer: ??? EXACT square 4cm, then triangle on top

3. Order is part of the meaning

"Put on socks, then shoes" and "put on shoes, then socks" use the same words and give very different results. Steps run top to bottom, one at a time. If you swap two lines, the result can change a lot. The computer won't warn you. It just does line 1, then line 2, then line 3.

4. Your turn (no code)

Here are directions to a friend's place. One step is too vague for someone who takes every word literally. Which one is it, and how would you fix it?

  1. Walk to the end of the street.
  2. Turn toward the tall building.
  3. Go to the third door.

Answer: step 2. "Toward the tall building" assumes the person can see the building and knows which one you mean. An exact step would be "turn left." Everything you write for a computer has to pass the same test.

5. Takeaway

A computer follows exact steps, in order, with no guessing. When you write an algorithm, your one job is to leave it no gaps and no chance to read you wrong. A vague step breaks sooner or later. Aim for exact, even if it is boring.

On LeetCode

This lesson writes no code, so there is no LeetCode problem for it yet. Exact steps in the right order are what every accepted answer needs, and you write your first ones in lessons 6 and 7.

Reading as a guest

Every free lesson is open to read and run. A free account marks lessons done and shows where you are in the course.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy