Algorithms using Pseudocode Pseudocode In the mock and






















- Slides: 22

Algorithms using Pseudocode

Pseudocode In the mock and previous assessments, it is clear that you have struggled with using Pseudocode or writing algorithms in general. In an exam, you may be asked to run through an algorithm and show the result. You may also be asked to write lines of Pseudocode for an algorithm.

Pseudocode is used to write an algorithm in programming-style constructs but it is not an actual programming language. Unlike using Python, you don’t have to worry about detailed syntax or be precise about how the code will do something. You just need to describe the steps you will need in your algorithm.

Pseudocode is line by line instructions but uses special command words. Rather than writing it entirely in English, there is a set of words that you need to use. Pseudocode is a mix between English and coding.

Basic Programming Constructs To help introduce you to Pseudocode, we are going to look at three different programming constructs • Sequence • Selection • Iteration

Sequence In your books, put the subtitle Sequence A sequence is writing the steps in the order they need to happen. For example… Input the product price Input the quantity Calculate total quantity x price Output the total price

Standardised Pseudocode The example provided is perfectly acceptable Pseudocode. However there is also something called Standardised Pseudocode which is the preferred format. Example Input the product price Input the quantity Calculate total quantity x price Output the total price Standardised Pseudocode product. Price USERINPUT quantity USERINPUT total quantity * product. Price OUTPUT “Total price is”, total I have printed a copy for your book.

Glossary Standards • • • USERINPUT OUTPUT IF ……. . THEN ELSE IF ………THEN ELSE ENDIF FOR ENDFOR REPEAT UNTIL WHILE ENDWHILE • Use <- to set value in a variable e. g: quantity <- USERINPUT • Use = with IF e. g: IF name = “Fred” • Put OUTPUT text in “ “ e. g: OUTPUT “ You have ordered “, quantity • Use CAPITAL LETTERS for instructions • Indent the same as you would in Python

Selection In your books, put the subtitle Selection uses conditional statements to decide which lines of code to run.

IF… THEN IF game. Won = True THEN (instructions here) ENDIF

IF… THEN… ELSE IF z <= 10 THEN z z + 10 ELSE z z – 10 END IF

IF… THEN… ELSE IF IF menu. Choice = 1 THEN display rules ELSE IF menu. Choice = 2 THEN play game ELSE IF menu. Choice = 3 THEN exit ENDIF

Full example An algorithm to check a user name and password are correct. username USERINPUT IF username = “Mr. King” THEN password USERINPUT IF password = “isthebest” THEN OUTPUT “Access Granted” ELSE OUTPUT “Access Denied” ENDIF ELSE OUTPUT “Invalid Username” END IF

Starter Tasks • Write the pseudocode for the programs on the sheet

Task Question 9 worksheet I have provided the first half of the program. You need to complete it by writing the second half below it in your book. IF element in list. A THEN IF element in list. B THEN OUTPUT "element is in both lists" ELSE OUTPUT "element is in List A" ENDIF ELSE

Solution IF element in list. A THEN IF element in list. B THEN OUTPUT "element is in both lists" ELSE OUTPUT "element is in List A" ENDIF ELSE IF element in list. B THEN OUTPUT "Element in list. B" ELSE OUTPUT "Element not in either list" ENDIF

Iteration Subtitle in your book Iteration is a loop in a program. We will look at three different ways of constructing a loop.

FOR… ENDFOR loop This executes a loop a set number of times FOR count 1 TO 12 OUTPUT count * 3 ENDFOR What does this program do?

REPEAT… UNTIL loop Condition controlled – will carry on looping until something happens. This does the same as the previous program. count 1 REPEAT OUTPUT count * 3 count + 1 UNTIL count >12

WHILE… ENDWHILE loop Another condition controlled loop. This loop continues as long as the condition is true. x USERINPUT WHILE x ≠ “End” OUTPUT x x USERINPUT ENDWHILE

Task Read the top of the sheet very carefully. You can answer Question 10 directly on to the sheet. Question 12 should be answered after the sheet in your book. Extension – Produce a flowchart for your Q 12 Pseudocode

Solutions Q 10 – Just take away the IF, and just have the convert to lowercase line. Q 12 Count 0 REPEAT num USERINPUT IF num > 60 THEN Count + 1 ENDIF UNTIL num = -1 OUTPUT Count