Docsity

Pseudocode Tutorial: Algorithms and Data Structures - Prof. Akerman, Slides of Computer Science

Partial preview of the text

Download Pseudocode Tutorial: Algorithms and Data Structures - Prof. Akerman and more Slides Computer Science in PDF only on Docsity!

What are each of the following symbols?

Input / Output Process – Maths operations and assignment of variables Line – shows direction of flow Terminal – for start and stop Decision – change flow based on a decision Sub program – call a different function or procedure What are each of the following symbols?

Some preliminaries …

Data types You will use the following data types in your algorithms: Data type Description Example INTEGER A whole number 1475 , 0 , - REAL A number with a decimal point 56.75, 6.0, -2.456, 0. BOOLEAN Either TRUE or FALSE TRUE, FALSE CHARACTER A single alphabetic or numeric character 'a', 'K', '4', '@', '%' STRING A sequence of one or more characters "Jo Hobson", "123"

What do these symbols/keywords mean? Symbol / keyword Meaning Symbol / keyword Meaning < (^) Less than / (^) Divide <= Less than or equal to if elseif else Branch depending on condition > (^) Greater than switch case default Branch depending on case >= Greater than or equal to input() Get user input == (^) Equal to print() (^) Output to the user = Assignment for Repeat a set number of times != (^) Not equal to while (^) Repeat while a condition is true ***** Multiply do until Do a loop until a condition is true ^ Exponent str() Convert to a string + (^) Addition or concatonte int() (^) Convert to an integer

Boolean operators The following operators are used to compare two values

greater than = greater than or equal to < less than <= less than or equal to == equal to != not equal to

Boolean expressions A Boolean expression evaluates to TRUE or FALSE

  • (^) What do each of the following expressions evaluate to? - (^) 35 >= 5 * 7 True - (^) 'A' < 'B' True - (^) 100 > 10^2 False - (^) 25 <= 2 * 5 False - (^) 2^4 == 16 True - (^) n^2 == n * n True
  • Worksheet
  • • Now complete Task 1 and Task 2 on Worksheet

Introducing pseudocode Pseudocode is a kind of structured English for describing algorithms

  • (^) It allows a programmer to focus on the logic of the algorithm without being distracted by the exact 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
  • (^) You may see the exam board style of pseudocode referred to as OCR Exam Reference Language or ERL

Programming constructs Last lesson we covered three basic ways of controlling the flow of a program:

  • (^) Sequence
  • (^) Selection
  • (^) Iteration What do each of these mean?

Selection

  • (^) An IF statement is a type of selection statement
  • (^) The next statement to be executed depends on whether the condition being tested is TRUE or FALSE hoursPerNight = int(input("How many hours a night do you sleep?")) if hoursPerNight < 8 then print("That’s not enough!") else print("That’s plenty!") endif Execute this if TRUE Execute this if FALSE

The switch/case statement

  • (^) The switch/case statement is used if there are several possible options to be tested switch optionChosen: case 1: print("You chose option 1") case 2: print("You chose option 2") case 3: print("You chose option 3") default: print("Please select a valid choice") endswitch Execute if optionChosen is 1 Execute if optionChosen is 2 Execute if optionChosen is 3 Execute if no cases match

Assignment pseudocode

  1. Write a statement of pseudocode to subtract discount from markedPrice to give salePrice
    • (^) salePrice = markedPrice - discount
  2. Write a statement of pseudocode to change the value stored in total to include VAT (at 20%)
    • (^) total = total * 0.2 + total OR
    • (^) total = total * 1. OR
    • (^) const VAT = 0. total = total * (1 + VAT) Simple correct solution Rewritten to require fewer operations Rewritten to make use of a constant

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("Please enter your name")
  • (^) This is equivalent to two statements: print("Please enter your name") firstName = input()