Module 4 · Beginner · How code works

Pseudocode and flowcharts for beginners

Pseudocode and flowcharts: how to write pseudocode, standard flowchart symbols, examples with conditions and loops, and turning both into Python code.

By theprogrammers team · · 9 min read

Your exam paper says "Write an algorithm and draw a flowchart to find whether a number is even or odd". Or you're about to code something tricky and want to plan it without getting tangled in Python syntax. Both cases call for the same tools: pseudocode and flowcharts. Pseudocode is your plan written in plain, code-like English. A flowchart is the same plan drawn as boxes and arrows.

Neither one runs on a computer. They exist so that you (and your teammates, teachers or interviewers) can see the logic clearly before any code is written. This lesson shows how to write pseudocode, the standard flowchart symbols, worked examples with conditions and loops, and how to turn both into working Python.

What you need first: what is an algorithm? and think like a programmer.

Key terms

  • Pseudocode: a description of an algorithm in structured plain language that looks a bit like code but follows no strict syntax.
  • Flowchart: a diagram of an algorithm, using standard shapes for each kind of step, joined by arrows.
  • Sequence: steps that run one after another.
  • Selection (decision): choosing between paths, like if and else.
  • Iteration (loop): repeating steps while a condition is true or for each item.
  • Dry run (trace): following an algorithm by hand with sample values to check it works.

Why plan before coding?

  • You think about logic, not syntax. No missing colons or brackets to distract you.
  • Mistakes are cheap to fix. Crossing out a line of pseudocode is quicker than rewriting a program.
  • Anyone can read it. A teammate who uses Java and one who uses Python can both follow the same pseudocode.
  • Exams and interviews use it. Many school and university exams ask for pseudocode or flowcharts, and interviewers often ask you to explain your approach before you code.

For a five-line program, you won't need either. For anything with several conditions or nested loops, a few minutes of planning saves a lot of debugging.

Writing pseudocode

There's no single official pseudocode. Common conventions keep it readable:

IdeaPseudocodePython
Start and endSTART ... END(the file)
InputINPUT marksmarks = int(input())
OutputOUTPUT "Pass" or PRINTprint("Pass")
Store a valueSET total TO 0 or total ← 0total = 0
DecisionIF ... THEN ... ELSE ... END IFif ...: ... else: ...
Loop while trueWHILE ... DO ... END WHILEwhile ...:
Loop a fixed number of timesFOR i FROM 1 TO 10 ... END FORfor i in range(1, 11):
Loop over itemsFOR EACH item IN list ... END FORfor item in items:

Rules of thumb:

  • Write keywords in CAPITALS so the structure stands out.
  • Indent the steps inside IF, WHILE and FOR.
  • One action per line.
  • Be exact about values ("marks ≥ 40", not "marks are good"), but don't worry about language details like data types.

Example: even or odd

START
    INPUT number
    IF number MOD 2 = 0 THEN
        OUTPUT "Even"
    ELSE
        OUTPUT "Odd"
    END IF
END

MOD means the remainder after division, which is % in Python. Turned into Python, with a fixed number instead of input() so you can see the result:

number = 17
if number % 2 == 0:
    print("Even")
else:
    print("Odd")
Odd

Each pseudocode line became one Python line. That's the aim: pseudocode detailed enough that the code almost writes itself.

Flowchart symbols

ShapeNameUsed for
Oval (rounded rectangle)TerminatorStart and End
ParallelogramInput/outputInput marks, Print total
RectangleProcessCalculations and assignments: total = total + mark
DiamondDecisionA yes/no question: marks ≥ 40?
ArrowFlow lineShows which step comes next
Small circleConnectorJoins parts of a chart across a page

A decision diamond always has (at least) two arrows out, labelled Yes and No (or True and False). Every flowchart has one Start and at least one End, and every path should lead to an End.

Even or odd as a flowchart

Drawn as text here; on paper, use the shapes from the table:

        ( Start )
            |
     / Input number /
            |
     < number MOD 2 = 0? >
       |Yes          |No
  / Print "Even" /  / Print "Odd" /
       |              |
       +------+-------+
              |
          ( End )

Free tools like draw.io (diagrams.net) or Excalidraw let you draw neat flowcharts with the real shapes.

Selection with several branches: grades

Pseudocode:

START
    INPUT marks
    IF marks ≥ 75 THEN
        grade ← "A"
    ELSE IF marks ≥ 60 THEN
        grade ← "B"
    ELSE IF marks ≥ 40 THEN
        grade ← "C"
    ELSE
        grade ← "F"
    END IF
    OUTPUT grade
END

In a flowchart, this is a chain of diamonds: marks ≥ 75? Yes goes to "grade = A"; No goes to the next diamond marks ≥ 60?, and so on. All branches join again before the output box.

Python:

for marks in [82, 64, 40, 12]:
    if marks >= 75:
        grade = "A"
    elif marks >= 60:
        grade = "B"
    elif marks >= 40:
        grade = "C"
    else:
        grade = "F"
    print(marks, grade)
82 A
64 B
40 C
12 F

Loops: sum of numbers from 1 to n

Pseudocode:

START
    INPUT n
    total ← 0
    i ← 1
    WHILE i ≤ n DO
        total ← total + i
        i ← i + 1
    END WHILE
    OUTPUT total
END

Flowchart: a loop is a decision diamond with an arrow that goes back up:

        ( Start )
            |
       / Input n /
            |
    [ total = 0, i = 1 ]
            |
   +---> < i ≤ n? > ---No---> / Print total / ---> ( End )
   |        |Yes
   |  [ total = total + i ]
   |        |
   |   [ i = i + 1 ]
   |        |
   +--------+

Dry run

Before coding, trace it by hand with n = 4:

ii ≤ 4?total after the step
1Yes1
2Yes3
3Yes6
4Yes10
5NoStop, output 10

Python:

n = 4
total = 0
i = 1
while i <= n:
    total = total + i
    i = i + 1
print(total)
10

The dry run and the program agree. A dry-run table is the best way to find mistakes such as i < n instead of i ≤ n, which would give 6 instead of 10.

A bigger example: ATM withdrawal

Planning real logic in pseudocode first makes the conditions clear:

START
    INPUT balance, amount
    IF amount ≤ 0 THEN
        OUTPUT "Enter a valid amount"
    ELSE IF amount MOD 100 ≠ 0 THEN
        OUTPUT "Amount must be a multiple of 100"
    ELSE IF amount > balance THEN
        OUTPUT "Insufficient balance"
    ELSE
        balance ← balance - amount
        OUTPUT "Please collect your cash"
        OUTPUT balance
    END IF
END

Python:

def withdraw(balance, amount):
    if amount <= 0:
        return balance, "Enter a valid amount"
    elif amount % 100 != 0:
        return balance, "Amount must be a multiple of 100"
    elif amount > balance:
        return balance, "Insufficient balance"
    else:
        return balance - amount, "Please collect your cash"

for amount in [2000, 250, 9000, -5]:
    print(amount, withdraw(5000, amount))
2000 (3000, 'Please collect your cash')
250 (5000, 'Amount must be a multiple of 100')
9000 (5000, 'Insufficient balance')
-5 (5000, 'Enter a valid amount')

Every branch of the pseudocode is tested by one of the four amounts.

Pseudocode or flowchart?

PseudocodeFlowchart
Quick to writeYesSlower to draw
Easy to changeYesRedrawing takes time
Shows the overall flow at a glanceLess soYes, very visual
Good for large programsYesGets messy
Common inInterviews, planning, textbooksSchool exams, explaining to non-programmers, business processes

Most working programmers use quick pseudocode (often just comments in the editor) far more than flowcharts. Flowcharts are handy for explaining a process to someone, or for seeing all the paths through tricky logic.

A neat trick: write your pseudocode as comments in the file, then fill in the code under each comment.

# get the list of prices
prices = [120, 45, 300]
# add them up
total = sum(prices)
# add 5% delivery charge
total = total * 1.05
# show the total
print(round(total, 2))
488.25

Common mistakes

  • Pseudocode that's too vague: "process the data" doesn't say what to do.
  • Pseudocode that's really Python: if it has colons and brackets everywhere, it's code, not a plan.
  • Diamonds with one exit, or unlabelled Yes/No arrows.
  • Loops with no way out: forgetting the step that changes the loop variable, like i ← i + 1.
  • Skipping the dry run: the trace table is where most logic mistakes are found.

Practice

  1. Write pseudocode that takes two numbers and outputs the larger one.
Show the answer
START
    INPUT a, b
    IF a > b THEN
        OUTPUT a
    ELSE
        OUTPUT b
    END IF
END

If they're equal, it outputs b, which is the same value, so that's fine.

  1. Which flowchart shape would you use for each: "Is age ≥ 18?", "Print ticket", "price = price × 0.9", "Start"?
Show the answer

Diamond (decision), parallelogram (output), rectangle (process) and oval (terminator).

  1. Do a dry run of this pseudocode and give the output.
count ← 0
FOR EACH n IN [5, 12, 7, 20]
    IF n > 6 THEN
        count ← count + 1
    END IF
END FOR
OUTPUT count
Show the answer

5: not greater than 6, count 0. 12: yes, count 1. 7: yes, count 2. 20: yes, count 3. Output: 3.

  1. Turn this pseudocode into Python and run it for n = 5.
INPUT n
factorial ← 1
FOR i FROM 1 TO n
    factorial ← factorial × i
END FOR
OUTPUT factorial
Show the answer
n = 5
factorial = 1
for i in range(1, n + 1):
    factorial = factorial * i
print(factorial)
120

range(1, n + 1) is needed because Python's range stops before its end value.

  1. This pseudocode should print 1 to 5 but never stops. Find the bug.
i ← 1
WHILE i ≤ 5 DO
    OUTPUT i
END WHILE
Show the answer

i never changes, so i ≤ 5 is always true. Add i ← i + 1 inside the loop, after OUTPUT i.

Key takeaways

  • Pseudocode is a plan in structured plain language; flowcharts draw the same plan with standard shapes.
  • Use capital keywords (IF, WHILE, FOR EACH, OUTPUT), indentation, and one action per line.
  • Flowchart shapes: oval for start/end, parallelogram for input/output, rectangle for processing, diamond for decisions.
  • Dry-run your plan with a trace table before coding; it catches most logic mistakes.
  • Good pseudocode turns into code almost line for line.

Frequently asked questions

Is there a standard pseudocode syntax?

No universal one. Exam boards and textbooks each have their own conventions, so follow the one your course uses. Outside exams, any clear, consistent style is fine.

Do professional programmers use flowcharts?

Not for everyday coding, but they use similar diagrams to explain processes, user flows and system designs. Pseudocode, often as comments or on a whiteboard in interviews, is used much more.

What is the difference between an algorithm and pseudocode?

The algorithm is the method itself. Pseudocode is one way of writing it down, alongside plain numbered steps, a flowchart or real code.

What to learn next

  1. Start the next module with how the internet works.
  2. Practise loops in real code with Python loops.
  3. Plan and solve harder problems with how to solve coding problems.