Introduction to Problem Solving
Sequence and selection
Use IF and ELSE in pseudocode to make a decision.
The flow of control is the order in which the steps of an algorithm happen. Steps can run in sequence, branch on a decision, or repeat.
In sequence, every step runs one after another, in order, with none skipped, as in the square example of the last lesson.
In selection, a condition decides what happens next. A condition is either true or false (only two values, like binary). For example: a person can get a driving licence if their age is 18 or more; otherwise, not.
It is written as: IF <condition> THEN do this, ELSE (otherwise) do that. For example, to say whether a number is positive:
- IF number > 0 THEN PRINT "Positive"
- ELSE PRINT "Not positive"
Conditions can be combined with AND (both must be true) and OR (at least one must be true). For example: a student gets a prize IF marks >= 90 AND attendance >= 75; otherwise, no prize.
When there are more than two possibilities, use ELSE IF to check one condition after another:
- IF marks >= 75 THEN grade = "A"
- ELSE IF marks >= 50 THEN grade = "B"
- ELSE grade = "C"
The conditions are checked in order, and the first one that is true decides the result. The book's Dragons and Wizards card game is another example, combining several conditions with OR and AND.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.