GoalTell sequential, conditional and loop algorithms apart and read a flowchart for each.
New words
sequential · steps done one after another, with no decision and no repetitionconditional · an algorithm that chooses a way using a questionloop · an algorithm that repeats stepscounter · a variable that counts the repetitions
Explanation
Most algorithms are built from three kinds of structure. A sequential algorithm does its steps one after another: ask for a name, read it, greet. A conditional algorithm asks a question and chooses a way: is the number positive or not? A loop repeats steps: show Hello five times. Many real programs mix the three. In the loop, a counter starts at 0 and the test is i < 5, so the counter takes the values 0, 1, 2, 3 and 4, which makes five turns. This is why for i in range(5) also gives 0 to 4 and not 1 to 5. One more detail: for the number 0 the positive-or-not program shows Not positive, because 0 is not larger than 0. Our message says “not positive”, not “negative”, to stay true.
Kind
What it does
Example
Sequential
steps one after another
ask a name, then greet
Conditional
chooses a way with a question
positive or not positive
Loop
repeats steps
show Hello five times
Worked examples
Sequential: the flowchart has no diamond and no arrow that goes back. It shows a message, reads a name and shows a greeting, in this order.
Conditional: the diamond asks num > 0. The user types 5 and gets Positive number. The user types 0 and gets Not positive, because 0 > 0 is false.
Loop: the counter i starts at 0. While i < 5, the program shows Hello and adds 1 to i. When i becomes 5 the test fails and the loop ends.
Class activity
Decide the kind of each algorithm: (a) read two numbers, add them, show the sum; (b) if it is raining take an umbrella, otherwise take a hat; (c) write your name ten times. Then write one idea of your own for each kind.
Practice
1
The user types Sami in the program in the picture. What does the screen show?
Python
1print("Enter your name:")
2name = input()
3print("Hello", name)
Enter your name:, then the typed name, then Hello Sami.
2
In the picture, how many times is Hi shown?
Python
1for i inrange(3):
2print("Hi")
Three times, because range(3) gives 0, 1, 2.
3
What does the positive-or-not program show for -4?
Not positive, because -4 > 0 is false.
4
Which kind of algorithm is “show Hello five times”?