Algorithms - Pseudocode
Jamie Z 2024-02-05Learning Goals
- I am able to understand algorithm constructs
- I am a able to communicating using Pseudocode
What are Algorithm Constructs?
There are basic building blocks that an algorithm consists of. Each of them serve a particular purpose
- Sequence
- Assignment - Assigning a value to a variable
- Selection and Condition - If and else if statements
- Iteration - Repeat a block of code
- Modularization - Never write the same code again and again
What is Pseudocode
Pseudocode will be used as the formal method of symbolizing algorithms
Pseudocode can be used for
- Demonstrating thinking that later can become comments in the final program
- Describing how an algorithm should work
- Explaining a computing process to less technical people
- Generating code in collaboration with others.
Pseudocode does not have a standard format and varies from programmer to programmer. However, a number of conventions are generally used. QCAA has mandated certain conventions.
**Key words**
IF, THEN, ELSE ENDIF OUTPUT INPUT FOR LOOP END FOR WHILE LOOP END WHILE CALCULATE EQUAL TO GREATER THAN GREATER THAN AND EQUAL TO SET DECLARE TO THEN LESS THAN EQUAL TO- A sequence is a number of instructions that are processed one after the other.
- For example, a very simple algorithm for brushing teeth might consist of these steps:
- Each step is an instruction to be performed. Sequencing is the order in which the steps are carried out.
DECLARE count
SET count TO 1
OUTPUT the value of count
Why is sequencing important?
- It is crucial that the steps in an algorithm are performed in the right order - otherwise the algorithm will not work correctly.
Example
- Grab the bowl
- Get cereal box from cupboard
- Pour cereal out of box in the bowl
- Obtain the milk bottle from the fridge
- Pour milk into the bowl
Assignment
- Assignment is used to store the value of an expression into a variable.
- They are similar to the variables that you would have seen in maths e.g. x=5.
- In programming, this is exactly the same idea, except computers can remember anything not just numbers, and additionally, the variables are generally given more meaningful names that relate to the data they contain e.g. Name = "Tom".
- Assignment is the way that a variable is told to remember a certain value.
DECLARE studentname
SET studentname TO Bob
Selection and Condition
- A selection statement uses a condition to select, or determine, the next line of the program that is to be executed.
- These statements help to make decisions and change the flow of a program. In many languages, there are three types of selection statements:
IF Statement
- The 'if' statement is one of the most basic and simplest controls flow statements. We can use the ‘if’ statement when we want to execute a group of one or more script statements only when a particular condition is met.
IF ELSE Statement
- As we know, the ‘if’ statement allows us to execute a set of statements only when a particular condition is true. However, if we want to execute another set of statements when the condition is false, then we can use the ‘if....else’ statement.
CALCULATE totalCost
IF (totalCost > \(10) \)THEN
OUTPUT ‘no delivery charge’
ELSE
OUTPUT ‘\(5 delivery charge’\)
ENDIF
OUTPUT ‘thankyou’
Iteration
- Iteration is also called repetition and is used when something is repeated over and over again, so anything where the program goes round in a loop
- A FOR loop is used when it is known exactly how many times to go round the loop
- A WHILE loop is used to repeat while a condition is true or false
Modularisation
- Modularisation is used to reduce the complexity of a system by deconstructing it into more or less independent units or modules. We are going to use the term "function" as that is what they are called in most of the predominant programming languages of today. Functions are important because they allow us to take large complicated programs and to divide them into smaller manageable pieces and more easily maintained.
FUNCTION delivery_charge
CALCULATE totalCost
IF (totalCost > \(10) \)THEN
OUTPUT ‘no delivery charge’
ELSE
OUTPUT ‘\(5 delivery charge’\)
ENDIF
OUTPUT ‘thankyou’
END FUNCTION
Activity
💡
Its ok, to a lesser extent - Nguyen C_,_ 2024