Flowcharts and Pseudo Code
Overview We will explore simple algorithms using flowcharts and pseudo code We will look at: Sequencing – putting things in the right order Conditions – doing different things depending on some rules Iteration (Looping) – repeating something Flowcharts and Pseudo Code
What is Pseudo code? Pseudo code is an informal, high-level, natural-language description of an algorithm. E.g. Start get value result = value * 100 display result Stop Flowcharts and Pseudo Code
What is a Flowchart? A flowchart is a graphical representation of an algorithm It uses standard shapes including: Tools: MS Visio, Word etc. Terminator Input or output Flow line Process Flowcharts and Pseudo Code
Why use Pseudo Code or Flowcharts? They are more concise and clear than a paragraph of writing They show the logical branching with indentation or flow lines They are not specific to a particular programming language They can be used to plan a solution before coding it They can be checked by or discussed with someone who cannot program Computers and Programming
Pseudo Code Pseudo code is informal but we will use this basic structure: Start get value result = value * 2 display result Stop The indentation is important… To begin To ask the user for some data (input) To do some operation To show something on the screen (output) To finish Flowcharts and Pseudo Code
Flowchart To begin To ask the user for some data (input) To do some operation To show something on the screen (output) To finish Flowcharts and Pseudo Code
Simple Sequencing Problem: calculate the average of three input numbers. How do we do this? Start get n1 get n2 get n3 average = (n1 + n2 + n3)/3 display average Stop n1 represents the first number entered average will contain the result of the sum: (n1 + n2 + n3) / 3 Flowcharts and Pseudo Code 8 of 33
Simple Sequencing Here is the flowchart for the same problem Remember to use the right shapes for input or output for a process or calculation Flowcharts and Pseudo Code
Conditions Conditions are the way we make a choice between two or more alternatives. Doing something different depending on a condition is called altering the flow of control. Start get mark if mark >= 50 result = “Pass” else result = “Fail” end-if display result Stop 1. Evaluate the condition getting True or False 2. If the result is True then perform these operation(s) 3. If it is False then perform these operation(s) Flowcharts and Pseudo Code
A Note on Meta-Symbols Note that the < and > characters are called meta-symbols What lies between then indicates the kind of thing which is allowed in its place. if <condition> then <operations for then-part> else <operations for else-part> end-if <variable> = <expression> A condition must go here. E.g. number < 10 A variable must go here. E.g. salary An expression must go here. E.g. basic + bonus 120 + 10 Flowcharts and Pseudo Code 11 of 33
Variable? What’s That? Variables are used to represent some data that might change, e.g. name = “Fred” age = 16 Constants are similar but their values don’t change, e.g. PI = 3.14 FILE_NAME = “mydata.txt” Variables start with a lower case letter Constants are always in UPPERCASE 12 of 33
Conditions Problem: Work out whether the student has passed or failed Start get mark if mark >= 50 result = “Pass” else result = “Fail” end-if display result Stop Flowcharts and Pseudo Code 13 of 33
Conditions There can be more than one expression in the <then> or <else> parts. E.g. Start Get mark if mark >= 50 then result = “Pass” message = “Well done!” else result = “Fail” message = “See me!” end-if display result display message Stop Flowcharts and Pseudo Code 14 of 33
Conditions It is also possible to have multiple conditions. Start get mark if mark < 50 then result = “Fail” else if mark < 60 then result = “Pass” else if mark < 70 then result = “Merit” else result = “Distinction” end-if display result Stop Flowcharts and Pseudo Code 15 of 33
Condition Flowchart 16 of 33
What does this do? What value is printed if: What is this doing? a=5, b=7, c=2 a=8, b=2, c=4 a=3, b=5, c=8 What is this doing? b a c Flowcharts and Pseudo Code 17 of 33
Your Turn Write some pseudo code and sketch a flow chart for the following cinema ticketing system: If customer is aged under 12 then the ticket £5 Otherwise, if they are over 65 the ticket is £7 Otherwise the ticket is £10 Flowcharts and Pseudo Code 18 of 33
Pseudo Code Start get age if age < 12: price = 5 else if age > 65: price = 7 else price = 10 end-if display price Stop Flowcharts and Pseudo Code 19 of 33
Cinema Ticket Flow Chart 20 of 33
Iteration If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display 2 etc. Stop Wouldn’t it be better to say something like this? number = 1 while the number <= 1000… display the number increase the number Flowcharts and Pseudo Code 21 of 33
Iteration: Display Numbers up to 1000 We can write this in pseudo code: Start number = 1 while number <= 1000 display number number = number + 1 end-while Stop Why do we need to do this? Flowcharts and Pseudo Code 22 of 33
Iteration: Display Numbers up to 1000 What is going on here? Flowcharts and Pseudo Code 23 of 33
Iteration: Get Five Numbers Problem: Allow the user to enter 5 numbers How do we know when we have 5 numbers? Start count = 0 while count < 5 get number count = count + 1 end-while Stop Why less than? Why do we need to do this? Flowcharts and Pseudo Code 24 of 33
Iteration: Get Five Numbers 25 of 33
Iteration: Total Five Numbers Now let’s also display the total of those numbers Start count = 0 total = 0 while count < 5 get number count = count + 1 total = total + number end-while display total Stop Here is the pseudo code to ask the user for five numbers We need a total, initially set to zero We need to add the number to that total At the end, we need to display the final total Flowcharts and Pseudo Code 26 of 33
Iteration: Total Five Numbers Flowcharts and Pseudo Code 27 of 33
Your Turn Write some pseudo code and sketch a flow chart to: Ask the user for ten numbers Display the total and average of these numbers Flowcharts and Pseudo Code 28 of 33
Average Ten Numbers Pseudo Code Start count = 0 total = 0 while count < 10 get number count = count + 1 total = total + number end-while average = total / count display total display average Stop Flowcharts and Pseudo Code 29 of 33
Average Ten Numbers Flow Chart 30 of 33
Iteration Types There are different ways of expressing iteration using pseudo code Which you use depends on what you are trying to do The programming language you intend to use is also likely to affect your choice We will use the first one because it translates most directly to Python while <condition> <operation(s) end-while do <operation(s) while <condition> repeat <operation(s)> until <condition> Flowcharts and Pseudo Code Flowcharts and Pseudo Code 31 of 33
Tracing a Flowchart Use a table of possible values and expected outcomes mark Expected result Actual result 49 Fail Fail 50 Pass Pass 59 60 Merit Merit 69 70 Distinction Distinction You must carefully consider the test values you need to validate your algorithm Flowcharts and Pseudo Code
Summary We have looked at why pseudo code and flowcharts are useful We have looked at pseudo code and flowcharts for sequencing, conditions and iteration On the VLE (Pseudocode and Flowcharts): FlowchartSymbols.pdf gives a summary of shapes PseudocodeNotes.pdf gives a summary of keywords Homework: Hodder’s OCR Computing for GCSE, page 156, questions 1 – 4 (due 17th October) Flowcharts and Pseudo Code 33 of 33