Algorithms - Pseudocode Flashcards
What is pseudocode?
A way of expressing algorithms in structured English that resembles a computer language.
Do you have to write your answers in a particular style of pseudocode?
No
Questions will be written using the OCR version
You may answer in any style as long as the meaning could be reasonably understood by a competent programmer
How is pseudocode similar to a computer language?
It uses similar commands, keywords and structures to those found in computer languages
A solution in pseudocode is converted into a programming language
Advantages of pseudocode. (2)
It is used to develop the logic ofthe algorithm without considering syntax
A human can follow the logic of an algorithm even if there are syntax errors, while a computer could not execute the same code
This makes it easier to first write a program in pseudocode
Disadvantage of pseudocode
It cannot be understood by computers
Variables in pseudocode
Same as python
parcel = “y”
Indefinite iteration in pseudocode
while (variable) = (value)
endwhile
There is nothing on the end of the line
The rest of the loop is indented after the line
endwhile is used to end the code
If statements in pseudocode
If (variable) = (value) then
OR
If (variable) = (value) then
else
endif
There is nothing else on the end of the line
“endif” is placed at the end of the code for the statement, still indented
“OR” is used in place of elif
“else” is also used
definite iteration
for (any word) = (num) to (num)
next (same word)
Any word/variable can be used, like i in python (e.g. index, goes)
next (variable) ends the section of code and is not indented
Comments in pseudocode
Marked with a //
Input in pseudocode
Same as python
variable = input(“text”)
Display as pseudocode an algorithm that will find the sum of 10 numbers entered by a user and output the result.
Use suitable comments and indentation. [6]
total = 0 //the variable total is used to store the sum of the numbers
for goes = 1 to 10 //a loop is set up to run from 1 to 10 as there will be 10 numbers
number = input(“Please enter the number.”) //the user is asked to enter a number
total = total + number //the total is calculated
next goes //closes the for loop
print(total) //the sum of the numbers is output