Skip to main content

How to think like a programmer

·4 mins

The problem #

Imagine I hand you a piece of paper with three numbers on it: 456, 123, and 789. Your task is to find their sum.

To a human, this looks like a single process. You might look at the numbers, scribble some carries on paper, and arrive at 1,368.

But as a programmer, you cannot treat this as a single action because a computer can only follow a specific set of instructions.

If you were to explain this to a machine that only follows one instruction at a time, how would you break it down?

Do you add all three numbers at once, or do you add them one by one?

How do you handle the carry?

Where do you store the intermediate total while you’re still working?

Human approach #

When you see three numbers like 456, 123, and 789 on a page, your brain performs a highly structured routine, even if it feels instantaneous.

You don’t look at the whole numbers, instead you focus on the rightmost column, the ones. You perform the sum: 6 + 3 + 9 and get 18. You write down 8 and remember 1 as a carry.

As you will see, remembering is a crucial part in programming.

Moving to the next column, you perform the sum: 5 + 2 + 8 + 1 (carry) and get 16. You write down 6 and remember 1 as a carry.

You repeat this loop until there are no columns left. You get 1368 as the final result.

Programmer approach #

To a computer, adding three numbers isn’t a single instruction, it is a repetitive process.

While the human approach focused on the vertical alignment of digits, the programmer’s approach focuses on iteration, and that’s because computers already know how to add two numbers at a time.

You treat the three numbers as a collection and use a running total to find the result.

First, you define the input as a collection of numbers (an array or a list). Basically, you tell the computer to set aside a small piece of memory to hold each number so that you can access them later.

SET numbers = [456, 123, 789]

Then you define a variable to hold the running total. In the human approach, this is like having a blank spot on your paper where you keep the current result. You start at zero because you haven’t added anything yet.

SET total = 0

And this is the loop instruction:

FOR EACH number IN numbers:

Instead of writing Add the first, then add the second…, you tell the computer to repeat a specific instruction for every number in the list.

This makes your logic scalable. It would work just as well for three numbers as it would for three million.

SET total = total + number

This instruction says: “Take the current value of the total, add the current number to it, and update the total with this new value”.

It’s important to read the = as assigns (or becomes) rather than equals.

Once the loop has finished visiting every number in the list, you can instruct the computer to display the final result.

DISPLAY total

Here’s the complete pseudocode (it is called like this because it’s not tied to any specific programming language):

SET numbers = [456, 123, 789]
SET total = 0

FOR EACH number IN numbers:
    SET total = total + number

DISPLAY total

Copy the above code and ask any AI to convert it to Python.
Then paste the result here and run it (press the Run button in the middle of the page).

Conclusion #

Thinking like a programmer is the practice of decomposing a task into discrete, repeatable steps.

By shifting from the manual process of adding numbers on paper to a formal, algorithmic loop, you move from solving a single instance to creating a general solution.

Focus on breaking down every problem you encounter into its smallest logical components. This is the foundation of software engineering.