☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Algorithms and flowcharts

الأسبوع 4 · الدرس ٢ «خوارزميات الذكاء الاصطناعي» (١): تعريف الخوارزمية وأشكال خريطة التدفق والخوارزميات التسلسلية والشرطية والتكرارية · 3 دروس
3

Sequential, conditional and loop algorithms

ICT Prep 3 Term 1, Unit 1, Lesson 2: pp. 19–22
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.

KindWhat it doesExample
Sequentialsteps one after anotherask a name, then greet
Conditionalchooses a way with a questionpositive or not positive
Looprepeats stepsshow Hello five times
Worked examples
Startshow: Enter your nameread nameshow: Hello nameEnd
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.
Startread numnum > 0yesnoPositive numberNot positiveEnd
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.
Starti = 0i < 5yesshow Helloi = i + 1noEnd
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)
2
In the picture, how many times is Hi shown?
Python
1for i in range(3):
2    print("Hi")
3
What does the positive-or-not program show for -4?
4
Which kind of algorithm is “show Hello five times”?