Pulsars
0 %
Log inSign up

Writing and Executing an Algorithm

Pseudocode, Your First Algorithm

Pseudocode, Your First Algorithm

To write an algorithm without worrying right away about a specific programming language, we use pseudocode: a way of writing the steps in structured plain language, with a few keywords (READ, IF, ELSE, DISPLAY...). Pseudocode cannot be executed by any computer, but it allows you to think clearly before coding.

Here is a classic algorithm: finding the largest of three numbers.

BEGIN
  READ a, b, c
  IF a > b AND a > c THEN
    max <- a
  ELSE IF b > c THEN
    max <- b
  ELSE
    max <- c
  END IF
  DISPLAY max
END

Let's break down this algorithm:

  • READ a, b, c: we get the three numbers to compare (the inputs).
  • IF ... THEN ... ELSE: we test conditions to decide which branch to execute.
  • max <- a: the symbol <- means that we store the value a in max (this is an assignment).
  • DISPLAY max: we show the result (the output).

The algorithm first tests whether a is the largest of the three. If not, it checks whether it's b. Otherwise, it must be c. Only one of these three branches will be executed, never all three: this is what's called a conditional structure.

This same algorithm, once translated into Python, would look like this:

a, b, c = 5, 9, 3
if a > b and a > c:
    max_valeur = a
elif b > c:
    max_valeur = b
else:
    max_valeur = c
print(max_valeur)

You can see that the logic is exactly the same: only the language used to write it changes.