Aqa Pseudo-code syntax Flashcards
IntExp, RealExp, BoolExp, CharExp and StringExp mean
IntExp, RealExp, BoolExp, CharExp and StringExp mean any expression which can be evaluated to an integer, real, Boolean (False or True), character or string respectively.
Exp means
Any expression
Emboldened pseudo-code is used
Used to indicate the keywords/operators
Exam paper questions will assume that indexing for arrays and strings start at __
0 unless specifically stated otherwise.
Single line comments
comment
Multi-line comments
# comment # comment and so on
Variable assignment
Identifier ← Exp
a← 3
b ← a + 1
c ← ‘Hello’
Constant assignment
CONSTANT IDENTIFIER ← Exp
CONSTANT PI ← 3.141 CONSTANT CLASS_SIZE ← 23 # Names of constants will always be # written in capitals
Standard arithmetic operations (4)
*
/
Used in the normal way with brackets to indicate precedence where needed. For example, a + b * c would multiply b and c together and then add the result to a, whereas (a + b) * c would add a and b together and then multiply the result by c.
Integer division
IntExp DIV IntExp
9 DIV 5
5 DIV 2
8 DIV 4
evaluates to 1
evaluates to 2
evaluates to 2
Modulus operator
IntExp MOD IntExp
9 MOD 5
5 MOD 2
8 MOD 4
evaluates to 4
evaluates to 1
evaluates to 0
Relational operators for types that can be clearly ordered:
Less than - Greater than - Equal to - Not equal to - Less than or equal to - Greater than or equal to -
Exp < Exp Exp > Exp Exp = Exp Exp ≠ Exp Exp ≤ Exp Exp ≥ Exp
Examples of:
Less than - Greater than - Equal to - Not equal to - Less than or equal to - Greater than or equal to -
4 <6 'A' < 'B' 'adam' < 'adele' 4.1 > 4.0 3= 3 qty ≠ 7 3 ≤4 4 ≤4 4≥ 3 4.5 ≥ 4.5
What is greater, and why?
‘adam’ ‘adele’
‘adam’ < ‘adele’
Alphabetically, ‘a’ comes before ‘d’, whichever first letter is different
Boolean operations
Logical AND
Logical OR
Logical NOT
BoolExp AND BoolExp
BoolExp OR BoolExp
NOT BoolExp
(3 = 3) AND (3 ≤ 4)
(x < 1) OR (x > 9)
NOT (a < b)
Indefinite (condition controlled) iteration
REPEAT-UNTIL (repeat the statements until the Boolean expression is True)
REPEAT # statements here UNTIL BoolExp
a←1 REPEAT OUTPUT a a←a+1 UNTIL a = 4
will output 1, 2, 3
Indefinite (condition controlled) iteration
WHILE
WHILE BoolExp
# statements here
ENDWHILE
a←1 WHILE a < 4 OUTPUT a a←a+1 ENDWHILE # will output 1, 2, 3
Definite (count controlled) iteration
FOR-TO-[STEP]- ENDFOR
If STEP IntExp is missing it is considered to be 1.
FOR Identifier ← IntExp TO IntExp [STEP IntExp]
# statements here
ENDFOR
# If STEP IntExp is omitted the step value is 1.
FOR a ← 1 TO 3
OUTPUT a
ENDFOR
What will this output?
will output 1, 2, 3
FOR a ← 1 TO 5 STEP 2
OUTPUT a
ENDFOR
What will this output?
will output 1, 3, 5
FOR-IN-ENDFOR (repeat the statements the number of times that there are characters
in a string)
FOR Identifier IN StringExp
# statements here
ENDFOR
Pseudocde to calculate the numbers of characters in message
length ← 0
FOR char IN message
length ←length +1
ENDFOR
# will calculate the # number of characters # in message
Pseudocde to output a string in revere
reversed ← '' FOR char IN message reversed ← char + reversed ENDFOR OUTPUT reversed
will output the string in reverse
SELECTION
IF-THEN-ELSE-ENDIF (execute the statements following the THEN if the Boolean expression is True, otherwise execute the statements following the ELSE)
IF BoolExp THEN
# statements here
ENDIF
a← 1 IF (a MOD 2) = 0 THEN OUTPUT ‘even’ ELSE OUTPUT ‘odd’ ENDIF
NESTED IF-THEN-ELSE ENDIF (use nested versions of the above to create more complex conditions)
Note that IF statements can be nested inside the THEN part, the ELSE part or both
IF BoolExp THEN # statements here ELSE IF BoolExp THEN # statements here ELSE # statements here ENDIF ENDIF
IF-THEN-ELSE IF ENDIF (removes the need for multiple indentation levels)
IF BoolExp THEN # statements here ELSE IF BoolExp THEN # statements here # possibly more ELSE ELSE # statements here ENDIF
ARRAYS ASSIGNMENT
Identifier ← [Exp, … ,Exp]
primes ← [2, 3, 5, 7, 11, 13]
Arrays
Accessing an elemenT
Identifier[IntExp]
primes ← [2, 3, 5, 7, 11, 13]
primes[0]
evaluates to 2
(questions on exam papers will start
indexing at 0 unless specifically stated
otherwise)
Arrays
Updating an element
Identifier[IntExp] ← Exp
primes ← [2, 3, 5, 7, 11, 13]
primes[5] ← 17 # array is now [2, 3, 5, 7, 11, 17]
Accessing an element in a two-dimensional array
Identifier[IntExp (row)][IntExp (column)]
table ← [[1, 2],[2, 4],[3, 6],[4, 8]]
table[3][1]
# evaluates to 8 as second element # (with index 1) of fourth array # (with index 3) in table is 8
Updating an element in a two- dimensional array
Identifier[IntExp][IntExp] ← Exp
table ← [[1, 2],[2, 4],[3, 6],[4, 8]]
table[3][1] ← 16
table[3][1] ← 16
# table is now #[ [1, 2], # [2, 4], # [3, 6], # [4, 16] ]
Array length
LEN(Identifier)
table ← [[1, 2],[2, 4],[3, 6],[4, 8]]
LEN(table)
evaluates to 4
table ← [[1, 2],[2, 4],[3, 6],[4, 8]]
LEN(table[0])
Evaluates to 2
FOR-IN-ENDFOR (repeat the statements the number of times that there are elements in an array)
NOTE: array items cannot be modified using this method
FOR Identifier IN array # statements here
ENDFOR
primes ← [2, 3, 5, 7, 11, 13] total ← 0 FOR prime IN primes total ← total + prime ENDFOR OUTPUT 'Sum of the values in primes is' OUTPUT total
Record declaration pseudo-code
RECORD Record_identifier
field1 :
field2 :
…
ENDRECORD
Record for a car declaration: make, model, reg, price, noOfDoors
RECORD Car make : String model : String reg : String price : Real noOfDoors : Integer ENDRECORD
Pseudo-code record variable initialisation
varName ←
Record_identifier(value1,
value2, …)
RECORD Car make : String model : String reg : String price : Real noOfDoors : Integer
Initialise a ford: pseudo-code
myCar ← Car(‘Ford’, ‘Focus’, ‘DX17
GYT’, 1399.99, 5)
Assigning a value to a field in a record - pseudo-code
varName.field ← Exp
myCar ← Car(‘Ford’, ‘Focus’, ‘DX17
GYT’, 1399.99, 5)
Change model to fiesta, pseudo-code
myCar.model ←'Fiesta' # The model field of the myCar # record is assigned the value # 'Fiesta'.
Accessing values of fields within records - pseudocode
varName.field
What does this do? (Records topic)
OUTPUT myCar.model
OUTPUT myCar.model
# Will output the value stored in the # model field of the myCar record
Difference between subroutines and functions in pseudo-code
for the purposes of this pseudo-code definition subroutines that contain a RETURN keyword are functions. Those that do not contain a RETURN keyword are procedures.
Subroutine definition
SUBROUTINE Identifier(parameters) # statements here ENDSUBROUTINE
Subroutine to add two numbers
SUBROUTINE showAdd(a, b)
result ← a + b
OUTPUT result
END SUBROUTINE
SUBROUTINE sayHi()
OUTPUT ‘Hi’
ENDSUBROUTINE
Function or procedure?
Procedure
SUBROUTINE add(a, b)
result ← a + b
RETURN result
Function or procedure?
Function
Calling subroutines without a return value
Identifier(parameters)
showAdd(2, 3)
Calling subroutines with a return value
identifier ← identifier(parameters)
answer ← add(2, 3)
String length pseudo-code
LEN(StringExp)
LEN(‘computer science’)
evaluates to 16(including space)
Position of a character
POSITION(StringExp, CharExp)
POSITION(‘computer science’, ‘m’)
# evaluates to 2 (as with arrays # exam papers will start # indexing at 0 unless # specifically stated otherwise)
Substring
Substring (the substring is created by the first parameter indicating the start position within the string, the second parameter indicating the final position within the string and the third parameter being the string itself).
SUBSTRING(IntExp, IntExp, StringExp)
SUBSTRING(2, 9, ‘computer science’)
evaluates to ‘mputer s’
Concatenation
StringExp + StringExp
‘computer’ + ‘science’
evaluates to ‘computerscience’
String and character conversion:
Converting string to integer - Converting string to real - Converting integer to string - Converting real to string - Converting character to character code - Converting character code to character -
STRING_TO_INT(StringExp) STRING_TO_REAL(StringExp) INT_TO_STRING(IntExp) REAL_TO_STRING(RealExp) CHAR_TO_CODE(CharExp) CODE_TO_CHAR(IntExp)
STRING_TO_INT(‘16’)
STRING_TO_REAL(‘16.3’)
16.3 INT_TO_STRING(16)
REAL_TO_STRING(16.3)
CHAR_TO_CODE(‘a’)
CODE_TO_CHAR(97)
STRING_TO_INT('16') # evaluates to the integer 16 STRING_TO_REAL('16.3') # evaluates to the real 16.3 INT_TO_STRING(16) # evaluates to the string '16' REAL_TO_STRING(16.3) # evaluates to the string '16.3' CHAR_TO_CODE('a') # evaluates to 97 using ASCII/Unicode CODE_TO_CHAR(97) # evaluates to 'a' using ASCII/Unicode
User input
USERINPUT
a ← USERINPUT
Output
OUTPUT a
OUTPUT a, g
The output statement can be followed by multiple StringExp separated by commas
Random integer generation
Random integer generation (between two integers inclusively)
Identifier ← RANDOM_INT(IntExp, IntExp)
diceRoll ← RANDOM_INT(1, 6)
Will randomly generate an integer between 1 and 6 inclusive