☰ Year plan · Communication Technology

Algorithms and Python (1)

Week 11 · Chapter 12 Programming (1): algorithms and flowcharts, Python basics: variables, arithmetic, input, loops and branching · 3 lessons
1

Algorithms and flowcharts

ICT Secondary 1 Term 1, Chapter 12, Lessons 12-1 to 12-3: pp. 155–167
GoalDescribe an algorithm as clear steps, read a flowchart, and name the three control structures.
New words
algorithm · a finite list of clear steps that solves a problemflowchart · a diagram of an algorithm made of shapes and arrowsbranching · choosing one of two paths by a conditionloop · repeating some steps
Explanation

An algorithm is a finite list of clear steps that solves a problem, like a recipe. Each step must be exact enough that anyone, or a machine, can follow it without guessing, and the algorithm must finish. A flowchart draws the algorithm. The rounded shape marks the start and the end, a slanted box (a parallelogram) is for input and output, a rectangle is a step of work, a diamond is a decision with two exits (yes and no), and arrows show the order. An activity diagram is the same idea drawn for a process. Every algorithm is built from three structures. Sequence: do the steps one after another. Branching: choose a path by a condition, such as “is n even?”. Loop: repeat steps while a condition holds. The first flowchart reads a number n and shows whether it is even (its remainder after dividing by 2 is 0) or odd. The second flowchart repeats: it adds 1, 2 and 3 to a total. Computers cannot guess, so the steps must be written in a programming language; machine language is the 0s and 1s that the processor runs directly, and a programming language such as Python is written for people and translated for the machine.

Startinput nn % 2 = 0yesnoshow evenshow oddEnd
Worked examples
Starttotal = 0, i = 1i ≤ 3yestotal = total + ii = i + 1noshow totalEnd
The loop flowchart. total starts at 0 and i at 1. While i ≤ 3 the box adds i to total and then adds 1 to i. The trace shows how total grows: after i = 1 it is 1, after i = 2 it is 3, after i = 3 it is 6; then i is 4, the condition is false, and the loop ends. The flowchart shows total = 6.
Passi at the starttotal afteri after
1112
2233
3364
The trace of the loop in a table (one row per pass). After the third pass the loop stops.
Class activity

Write an algorithm in plain steps for making tea or for finding the larger of two numbers. Mark which part is a sequence, which part is a branching and whether there is a loop. Then draw its flowchart on paper with the four kinds of shapes.

Practice
1
Which flowchart shape shows a decision?
2
Name the three structures of an algorithm.
3
Why must an algorithm finish?
4
In the loop flowchart, what is the value of total at the end?