gcse pseudocode ocr unit 5 algorithms computer ......ocr computer science j276 pseudocode unit 5...
TRANSCRIPT
GCSE OCR Computer Science J276
Pseudocode Unit 5 Algorithms
5
• Define data types integer, real, Boolean, character, string
• Define Boolean operators
• Understand and use sequence, selection and iteration in an algorithm
• Write algorithms in pseudocode involving sequence, selection and iteration
Objectives
Pseudocode Unit 5 Algorithms
Data types • You will use the following data types in your
algorithms:
Data type Description Example
INTEGER a whole number 1475, 0, -5
REAL a number with a decimal point 56.75, 6.0, -2.456, 0.0
BOOLEAN Can only be TRUE or FALSE TRUE, FALSE
CHARACTER A single alphabetic or numeric character ‘a, ‘K’, ‘4’, ‘@’, ‘%’
STRING One or more characters enclosed in quote marks ‘Jo Hobson’, ‘123’
Pseudocode Unit 5 Algorithms
Boolean operators • The following operators are used to compare two
values > greater than >= greater than or equal < less than <= less than or equal = equal (in Python this is written ==) <> not equal (in Python this is written !=)
Pseudocode Unit 5 Algorithms
Boolean expressions • A Boolean expression evaluates to TRUE or FALSE
• Which of the following expressions are TRUE? (i) 35 >= 5 * 7
(ii) ‘A’ < ‘B’
(iii) 100 <= 10**2
(iv) 25 > 2 * 5
(v) 2 ^ 4 = 16
(vi) n ^ 2 = n * 2
Pseudocode Unit 5 Algorithms
Boolean expressions • A Boolean expression evaluates to TRUE or FALSE
• Which of the following expressions are TRUE? (i) 35 >= 5 * 7 True
(ii) ‘A’ < ‘B’ True
(iii) 100 <= 10**2 True
(iv) 25 > 2 * 5 True
(v) 2 ^ 4 = 16 True
(vi) n ^ 2 = n * n True
Pseudocode Unit 5 Algorithms
Programming constructs • There are three basic ways of controlling the order in
which program statements are carried out
• You have already used examples of all of them:
• Sequence
• Selection
• Iteration
Pseudocode Unit 5 Algorithms
Sequence • The statements are executed in the order they are
written
mealCost=4.00drinkCost=2.00
total=mealCost+drinkcost
Pseudocode Unit 5 Algorithms
Selection • An IF statement is a selection statement
• The next statement to be executed depends on whether the condition being tested is true or false
hoursPerNight=(“Howmanyhoursanightdoyou sleep?”)
if(hoursPerNight<8)then
OUTPUT(“That’snotenough!”)else OUTPUT(“That’splenty!”)endif
Pseudocode Unit 5 Algorithms
The switch/case statement • The switch/case statement is used if there are
several possible options to be tested switchoptionChosen: case1: print(“Youchoseoption1”) case2: print(“Youchoseoption2”) case3: print(“Youchoseoption2”)endswitch
Pseudocode Unit 5 Algorithms
Introducing pseudocode • Pseudocode is a kind of structured English for
describing algorithms
• It allows the designer to focus on the logic of the algorithm without being distracted by the syntax of the programming language
• You will see pseudocode statements written in a consistent style in exam questions, but you can use alternative statements so long as the meaning is clear
Pseudocode Unit 5 Algorithms
Pseudocode for assignment • To assign a value to a variable, you can write
statements such as total=0cost=adult*2+child*3counter=counter+1
• How would you write a statement to subtract discount from markedPrice to give salePrice?
Pseudocode Unit 5 Algorithms
Pseudocode for input/output • Most programs that you will write will ask the user to
enter some data, and then accept the user input and assign it to a variable
• The pseudocode used in an exam will look like this: firstname=input(“Pleaseenteryourname”)
• This is equivalent to two statements: print(“Pleaseenteryourname”)
firstname=input
Pseudocode Unit 5 Algorithms
Inputting data - examples Write a single statement to do each of the following:
• Display a prompt “How old are you?” Accept an answer from the user Assign the answer to a variable called age
• Display a prompt “Press Enter to continue” When the user presses Enter, continue to the next statement
Pseudocode Unit 5 Algorithms
Inputting data - Answer Write a single statement to do each of the following:
• Display a prompt “How old are you?” Accept an answer from the user Assign the answer to a variable called age
age = input (“How old are you?”)
• Display a prompt “Press Enter to continue” When the user presses Enter, continue to the next statement
x = input (“Press Enter to continue”)
Pseudocode Unit 5 Algorithms
Writing pseudocode • Write pseudocode for a program which asks the user
to enter the cost of two items, adds the two costs and if the cost is greater than £10.00, displays a message “Sorry, too much”. Otherwise it displays the change due from £10.00
Pseudocode Unit 5 Algorithms
Pseudocode solution item1=input(“Pleaseenterpriceoffirstitem:”)item2=input(“Pleaseenterpriceofseconditem:”)total=item1+item2iftotal>10thenprint(“Sorry,toomuch”)
elsechange=10–item1-item2print(“Changefrom£10.00is£”,change)
endif
Pseudocode Unit 5 Algorithms
Flowchart or pseudocode? • Write pseudocode corresponding to this flowchart:
Input mark
Yes
No
Yes
No
print “Merit”
print “Pass”
print “Fail”
mark >=70?
mark >=50?
Pseudocode Unit 5 Algorithms
Iteration • Iteration means repetition
• There are three types of iteration statement in most, but not all, imperative programming languages such as Pascal, Visual Basic or Python
• for … next
• while … endwhile
• do … until
• Some languages (e.g. Python) do not have the do … until statement
Pseudocode Unit 5 Algorithms
for … next loop • Use this when you want to execute the loop a
specific number of times total = 0 for counter from 1 to 7
maxTemperature = input(“enter max temperature: ”) add maxTemperature to total
next counter averageWeeksTemp = total / 7 print (“This week’s average is ”, averageweekstemp)
Pseudocode Unit 5 Algorithms
while … end while • use this when you want to execute the loop while a
certain condition is true. emailaddress=input(“Enteremailaddress:”)whileemailaddressdoesnotcontain“@”
print(“invalidaddress–pleasere-enter”) emailaddress=input(“Enteremailaddress:”)endwhileprint(“thankyou”)
• the condition is tested at the beginning of the loop • how many times will the loop be executed if the user
enters [email protected]?
Pseudocode Unit 5 Algorithms
Flowchart while … end while emailAddress=input(Pleaseenteremailaddress”)whileemailAddressdoesnotcontain“@”emailAddress=input(“Invalid
address–pleasere-enter”)endwhileprint(“Thankyou”)
Input emailAddress
print “Invalid address – please
re-enter”
emailAddress contains @?
Input emailAddress
No
Yes
Pseudocode Unit 5 Algorithms
do … until • Use this when you want to execute the loop until a
certain condition is true
• The condition is tested at the end of the loop
• Can you rewrite this algorithm using a do loop instead of a while loop? emailAddress=input(Pleaseenteremailaddress”)whileemailAddressdoesnotcontain“@”emailAddress=input(“Invalidaddress–please re-enter”)endwhileprint(“Thankyou”)
Pseudocode Unit 5 Algorithms
Using do … until • The condition is not tested until the end of the loop
(it is always executed at least once), so you need an if statement as well as the do loop doemailAddress=input(Pleaseenteremailaddress”)ifemailaddressdoesnotcontain“@”then emailAddress=input(“Invalidaddress– pleasere-enter”)endif
untilemailAddresscontains“@”
Pseudocode Unit 5 Algorithms
Worksheet 5 • Complete Questions 1 - 4 on the worksheet
Copyright
© 2016 PG Online Limited
The contents of this unit are protected by copyright.
This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.
Licence agreement
This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to you by PG Online Limited for use under the terms of the licence.
The materials distributed with this unit may be freely copied and used by members of a single institution on a single site only. You are not permitted to share in any way any of the materials or part of the materials with any third party, including users on another site or individuals who are members of a separate institution. You acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.
Pseudocode Unit 5 Algorithms