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 ...
So that we can ...
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.
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.
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
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.
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. 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 ...
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.
First, open up the Python programming environment, IDLE. 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 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. 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 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. 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. Task 2.2 NestingPrint 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. 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!) 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. 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. Answer the following questions in your notebook / on paper
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.
Click to load key word list to help you make your own flash cards 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'. |