CS11 : Making choices


https://docs.google.com/presentation/d/1HnWxCelZo8ITm49b1XCBQnloX1_NIsSbcX8AiOvxv7g/preview
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

# Get Ready.png

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


Task 1.1 Flowchart symbols
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.

https://drive.google.com/file/d/0B83yXMOilskaMFFDUjhINEVraEE/preview
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.



Task 1.2 Children's Games
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


Task 2.1 Try these!
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.




Get ready to code

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.

https://drive.google.com/file/d/0B83yXMOilskaTTZzQmcxcy1XZ3M/preview
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.

https://drive.google.com/file/d/0B83yXMOilskaem9WcGp5eU5kR1E/preview
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.

https://drive.google.com/file/d/0B83yXMOilskaenpZTURzWENHUUE/preview
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.



Task 2.2 Nesting
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.



Get ready to code

Open up the Python programming environment, IDLE.


Download the script

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.

https://drive.google.com/file/d/0B83yXMOilskaWExTWFFWZEo2TUE/view?usp=drive_web

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

Answer the following questions 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?


# Faster Workers.png

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. 


Assessment Task (Homework)

This will test your flowchart drawing skills! Look back at the 'nesting.py' script which you used in Task 2.2 for which you carried out some boundary data tests. You're task is to draw a flowchart to represent the control flow in this script. HINT : You will need one START, one input, four decision diamonds, five outputs and one STOP.

If you are really struggling with this, ask your teacher for a template.

Grading rubric

MASTER :  You have created a suitable flowchart using the required symbols which correctly describes the control flow in the Python script. You have not used the template.
APPRENTICE You have attempted to create a flowchart but have made limited errors in your logic. You have not used the template to help you.
NOVICE : You have used the template to help you and have used it to correctly describe the control flow in the program.

Click to download revision cards
https://docs.google.com/document/d/17iepb9AqDb9hRHR44_O2ofuRbmEr7Cn87IgNgG3yacE/export?format=pdf
Remember to print them single sided

# Flash cards.png
Click to load key word list to help you make your own flash cards 

https://goo.gl/forms/db4pAQdVgdeQWMp92
Try to get 5/5! 


Hungry for more?

https://xkcd.com/518/
XKCD on flowcharts - what more could you wish for?

Frequently Asked Questions

Q : What does a 'subprocess' symbol do?
A : Good question - we'll found out more about that in a future topic (Break it down) but for now just think of it as a way of simplifying a complex flowchart by grouping together lots of operations under one 'block'.