CS11 : Making choices

There are three fundamental aspects to imperative programming - sequence, selection and iteration. Sequencing is natural - one thing after another. In this section, we look at the second of these, selection or making choices.

We are learning ...
• How computer programs make decisions
So that we can ...
• Appreciate the Boolean nature of decision making in computer programs
• Use practical examples of selection in Python

 Activity 1 Flowcharts I   O   A   E

We've not really spoken about flowcharts much yet although you may have seen reference to them during your work on algorithmic thinking or whilst learning to program.

Flowcharts are visual representations of algorithms

Where we learn about the different flowchart symbols

There are lots of flowchart symbols. I've picked out what I consider to be the most important ones and they are shown in the diagram below.

Click to engage
• Start / stop symbol : This symbol is used at the start and, sometimes, at the end of a flowchart.
• Process symbol : This symbol is used to represent either an assignment, mathematical or logical operation.
• Input / output symbol : As the name suggests, this symbol is used for input and output.
• Decision symbol : This symbol always contains a decision statement and has two separate outputs.
• Subprocess symbol : This symbol is used to represent a complex process which may be defined elsewhere.
The most complex component is the decision symbol because it involves a) the use of decision statements and because b) it involved conditional branching. Look carefully at the structure.

In your notebooks / on paper

Copy down all these flowchart symbols and underneath, or next to, each one, write a description of what it is designed to show. Pay particular attention to your description of a decision symbol.

Where we learn that children's games have an element of decision in them

Childrens games always involve decisions

Get copies of the three worksheets

Download and print, or get a copy off your teacher, of the three worksheets 'Hide and seek', 'Piggy in the middle' and 'You are it'. Each on of them contains a number of decision statements / symbols which are not fully labelled.

On the worksheets / in the room

Write either 'T' or 'F' into the correct boxes to indicate which route the algorithm should take in the event of each decision statement being True or False. When you have completed the worksheets, how about playing the game!

HEALTH AND SAFETY WARNING : Make sure you've got lots of room.

 Activity 2 Practical examples I   O   A   E

Selection in any programming language involved using decision statements (which is why we've been studying them). In Python, the only available selection statement is structured like this ...

 `if DECISION_STATEMENT:      # This is required.``  if_body                   # Commands which execute when DECISION_STATEMENT is True``[elif DECISION_STATEMENT:   # Optional conditional test - zero or more of these allowed!``  elif_body]*               # Commands which execute when DECISION_STATEMENT is True``[else:                      # Optional else clause - zero or one of these allowed!``  else_body]                # Commands which execute when every DECISION_STATEMENT is False`

Here, the square brackets indicate optionality and the * indicates that there may be zero or more occurrences.

VIDEO DEMO - BIGGER NUMBER

Your teacher might demonstrate this to you as well

Where we learn how to program some examples of selection statement in Python

For each section in this task, you will be asked to use the flowchart to create a Python script and then test it using the stated number of tests. Testing should prove the operation of every path of execution in a script.

First, open up the Python programming environment, IDLE.

In a script

Write a suitable input statement which asks the user for their name. If their name is "John", the script should print "Welcome John!". Carry out two tests to ensure that your script works correctly. Click on the flowchart to enlarge it.

Click to engage

In your notebook / on paper

Print out the flowchart and the script you have written for your notebook / folder. Describe the two tests
that you carried out and the results you got.

In a script

Write a suitable input statement which asks the user for a password. If the password they enter is the same as "tiddles" then print "Treasure unlocked" or else print "Sorry, no bananas". Carry out two tests to ensure that your script works correctly. Click on the flowchart to enlarge it.

Click to engage

In your notebook / on paper

Print out the flowchart and the script you have written for your notebook / folder. Describe the two tests that you carried out and the results you got.

In a script

Write a suitable input statement which asks the user for their age in whole years. Don't forget to convert the user input into an integer. If their age is less than 17, the script should print "Sorry, no driving for you!" or else, if their age is exactly 17, it should print "You should learn to drive this year" or else the script should print "Why haven't your learnt to drive already?". Carry out three tests to ensure that your script works correctly. Click on the flowchart to enlarge it.

Click to engage

In your notebook / on paper

Print out the flowchart and the script you have written for your notebook / folder. Describe the three tests that you carried out and the results you got.

Where we learn that you can put one decision statement inside another

There may be a situation where you wish to perform a selection only if the result of a previous selection is True (or, indeed False). In this situation, you might use nesting.

Open up the Python programming environment, IDLE.

Download the script called 'nesting.py' to a suitable place in your documents. Find the downloaded script, right click on it and choose 'Edit with IDLE' to open the script in the editor. Inspect the script and rubber duck it to your shoulder partner (quack!)

In your notebook / on paper

Take a screenshot of the script and paste it into a new word processed document and copy the following table underneath. Don't forget to put your name, class and the date at the top of the document.

Complete the 'Expected outcome' column without running the script.

Now, run the script once with each 'Test value' and record what happens in the 'Actual outcome' column. Compare this to what you suggested would happen and wrote in the 'Expected outcome' column - were you correct? This is called desk checking.

In your notebook / on paper

1. The test values in the table are called boundary tests. Can you explain why they are called boundary tests
2. There is one range of values in this script which is not tested. What is it?

Caveat on CASE ... SELECT / SWITCH ... CASE

Lots of other programming languages use `case ... select` or `switch ... case` constructs to enable multiple outcomes to exist from a list of choices. Python uses the `if ... [elif]* ... [else]` construct to replace this and does not have these programming constructs.

Remember to print them single sided