topic badge
CanadaON
Grade 7

15.06 Algorithms in code

Lesson

 

Previously we have learnt how to represent an algorithm (a sequence of steps) using a flow chart diagram. Each step of an algorithm can also be represented by a set of worded instructions called a line of code. There are many different representations of code, called programming language. These include Python, Scratch, Java, and C. We use programming language to communicate with computers to give them the instructions that we want them to perform.

A programming language usually consists of:

  • Statements: which give an instruction for the action - written as capital letters
  • Expressions: which represent a value
 

Pseudocode

Before translating an algorithm into a specific programming language, the steps of the algorithm can be written as pseudocode. Pseudocode is easy to read coding language that can’t be used by a computer but can be easy to follow for humans to check and make sure the program will work before writing into a specific computer language.

Common pseudocode notation

There is no strict set of standard notations for pseudocode, but some of the most widely recognised are:

  • INPUT – indicates a user will be inputting something
  • OUTPUT or PRINT – indicates that an output will appear on the screen
  • SET - assign an initial value to an expression
  • IFTHENELSE – a decision (selection) in which a choice is made

Any instructions that occur inside a decision are usually indented.

Worked example:

The following Algorithm adds two numbers:

This algorithm can be represented in pseudocode as:

START
SET a = 2
SET b = 3
PRINT a + b
END

Question: What is the OUTPUT of this code?

The steps of this code are:

  1. assign a the value 2
  2. assign b the value 3
  3. print the answer of a + b

So the OUTPUT of this code is 5

Making a decision

When a decision is to be made between two alternatives in code, we can use the IFTHENELSE statement.

Worked example:

The following algorithm determines which of two numbers is greater:

START
SET a = 8
SET b = 5
IF a >= b
    THEN PRINT a
ELSE PRINT b
END IF
END

Question: What is the OUTPUT of this code?

The steps of this code are:

  1. assign a the value 8
  2. assign b the value 5
  3. If a (8) is larger than or equal to b (5), then print a (8)
  4. Otherwise print b (5)

Since a is larger than or equal to b the OUTPUT of this code is 8.

Practice questions

QUestion 1:

Look at the pseudocode below

START
SET a = 73
SET b = 44
SET c = a + b
PRINT c
END

What value is printed at the end of the code?

  1. OUTPUT: $\editable{}$

QUESTION 2:

Look at the pseudocode below

START
SET a = 8
IF a / 2 IS integer
    THEN PRINT "a is even"
ELSE PRINT "a is odd"
END IF
END

What statement is printed at the end of the code?

Note: the symbol / represents division.

  1. a is even

    A

    a is odd

    B

QUESTION 3:

Consider the rectangle shown in the image.

The dimensions of the rectangle are $W=3$W=3 metres and $L=6$L=6 metres.

  1. What does the following pseudocode calculate?

    START
    SET W = 3
    SET L = 6
    SET A = W * L
    PRINT A "m2"
    END

    Note: the symbol * represents multiplication.

    Volume

    A

    Units

    B

    Area

    C

    Perimeter

    D
  2. What value is printed at the end of the code?

    OUTPUT: $\editable{}$ m2

 

Outcomes

7.C3.1

Solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code that involves events influenced by a defined count and/or sub-program and other control structures.

7.C3.2

Read and alter existing code, including code that involves events influenced by a defined count and/or sub-program and other control structures, and describe how changes to the code affect the outcomes and the efficiency of the code.

What is Mathspace

About Mathspace