Breaking a problem into steps: flowcharts and pseudocode

Video lessonComing soon

1 Steps first, code later

Many beginners freeze in front of a new problem. The usual reason is that they try to think about the steps and the language at the same time.

It helps to split the two. First we write the steps in plain words, with no language at all. Once the steps are right, turning them into C++ or Python is the easy part.

There are two common ways to write the steps down. A flowchart draws them as boxes and arrows. Pseudocode writes them as short lines of text. Both say the same thing. A flowchart is easier to see at a glance, and pseudocode is closer to real code.

2 The four flowchart shapes

A flowchart uses four shapes. The arrows between them show which step comes next.

ShapeMeansExample
Rounded boxWhere the steps start or endStart, End
Slanted boxInput or outputRead n, Print sum
RectangleOne step of worksum = 0
DiamondA question with a Yes or No answern > 0?

The diamond is the only shape with two arrows going out, one for Yes and one for No. That is how a flowchart makes a choice or repeats steps.

3 An example: the sum of the digits

Our problem is to find the sum of the digits of a number n. For n = 472, the answer is 4 + 7 + 2 = 13.

Two operations do the work. n % 10 gives the last digit, and n / 10 (whole-number division) removes it. So we keep taking the last digit, adding it to a running sum and dropping it, until no digits are left.

StartRead nsum = 0n > 0?sum = sum + n % 10n = n / 10Print sumEndNoYes
Yes repeats the two steps on the right, then asks again

The diamond asks "n > 0?". While the answer is Yes, we run the two steps on the right and come back to the question. When the answer becomes No, no digits are left and we print the sum.

Let us follow it for n = 472:

Stepnsumn > 0?
Start4720Yes
Add 2, drop it472Yes
Add 7, drop it49Yes
Add 4, drop it013No

The loop stops with sum = 13, which is the right answer.

4 The same steps as pseudocode

Pseudocode writes the flowchart as text. There is no fixed syntax, but a few habits keep it clear:

  • Write one step per line.
  • Indent the steps that sit inside a loop or an if.
  • Leave out language details such as types, brackets and semicolons.
read n
sum = 0
while n > 0
    sum = sum + (n % 10)
    n = n / 10          // whole-number division
print sum

Each line of the pseudocode matches one shape in the flowchart. The while line is the diamond, and the two indented lines are the steps on its Yes side.

A choice, the other use of a diamond, looks like this in pseudocode. Here it finds the larger of two numbers:

read a, b
if a > b
    print a
else
    print b
StartRead a, ba > b?Print aPrint bEndNoYes
One question, two paths, one end

5 From pseudocode to code

Now the language comes in. Each pseudocode line becomes one line of code. The read and print lines turn into the function's parameter and its return value.

The steps are the same in all seven. Only the spelling changes, such as how whole-number division is written.

6 A habit for every problem

Before writing code for any problem, take two minutes for these four steps:

  1. Write down what the input is and what the output must be.
  2. Write the steps in plain words, as pseudocode or a quick flowchart.
  3. Follow the steps by hand on one small example, the way we did for 472.
  4. Only then write the code, one pseudocode line at a time.

Step 3 catches most mistakes before they cost a submission. The next lesson, on dry runs, turns it into a proper method.

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.