Breaking a problem into steps: flowcharts and pseudocode
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.
| Shape | Means | Example |
|---|---|---|
| Rounded box | Where the steps start or end | Start, End |
| Slanted box | Input or output | Read n, Print sum |
| Rectangle | One step of work | sum = 0 |
| Diamond | A question with a Yes or No answer | n > 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.
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:
| Step | n | sum | n > 0? |
|---|---|---|---|
| Start | 472 | 0 | Yes |
| Add 2, drop it | 47 | 2 | Yes |
| Add 7, drop it | 4 | 9 | Yes |
| Add 4, drop it | 0 | 13 | No |
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
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:
- Write down what the input is and what the output must be.
- Write the steps in plain words, as pseudocode or a quick flowchart.
- Follow the steps by hand on one small example, the way we did for 472.
- 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.