Aqa Pseudo-code syntax Flashcards Preview

Computing p > Aqa Pseudo-code syntax > Flashcards

Flashcards in Aqa Pseudo-code syntax Deck (69)
Loading flashcards...
1
Q

IntExp, RealExp, BoolExp, CharExp and StringExp mean

A

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.

2
Q

Exp means

A

Any expression

3
Q

Emboldened pseudo-code is used

A

Used to indicate the keywords/operators

4
Q

Exam paper questions will assume that indexing for arrays and strings start at __

A

0 unless specifically stated otherwise.

5
Q

Single line comments

A

comment

6
Q

Multi-line comments

A
# comment
# comment and so on
7
Q

Variable assignment

A

Identifier ← Exp

a← 3
b ← a + 1
c ← ‘Hello’

8
Q

Constant assignment

A

CONSTANT IDENTIFIER ← Exp

CONSTANT PI ← 3.141 CONSTANT CLASS_SIZE ← 23
# Names of constants will always be
# written in capitals
9
Q

Standard arithmetic operations (4)

A

*
/

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.

10
Q

Integer division

A

IntExp DIV IntExp

11
Q

9 DIV 5
5 DIV 2
8 DIV 4

A

evaluates to 1
evaluates to 2
evaluates to 2

12
Q

Modulus operator

A

IntExp MOD IntExp

13
Q

9 MOD 5
5 MOD 2
8 MOD 4

A

evaluates to 4
evaluates to 1
evaluates to 0

14
Q

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 -
A
Exp < Exp
   Exp > Exp
   Exp = Exp
   Exp ≠ Exp
   Exp ≤ Exp
   Exp ≥ Exp
15
Q

Examples of:

Less than - 
Greater than - 
Equal to - 
Not equal to - 
Less than or equal to -  Greater than or equal to -
A
4 <6
'A' < 'B' 
'adam' < 'adele'
4.1 > 4.0 
3= 3
qty ≠ 7
3 ≤4 
4 ≤4
4≥ 3 
4.5 ≥ 4.5
16
Q

What is greater, and why?

‘adam’ ‘adele’

A

‘adam’ < ‘adele’

Alphabetically, ‘a’ comes before ‘d’, whichever first letter is different

17
Q

Boolean operations

Logical AND
Logical OR
Logical NOT

A

BoolExp AND BoolExp
BoolExp OR BoolExp
NOT BoolExp

(3 = 3) AND (3 ≤ 4)
(x < 1) OR (x > 9)
NOT (a < b)

18
Q

Indefinite (condition controlled) iteration

REPEAT-UNTIL (repeat the statements until the Boolean expression is True)

A
REPEAT
# statements here UNTIL BoolExp
a←1
REPEAT 
   OUTPUT a
    a←a+1 
UNTIL a = 4

will output 1, 2, 3

19
Q

Indefinite (condition controlled) iteration

WHILE

A

WHILE BoolExp
# statements here
ENDWHILE

a←1
WHILE a < 4
  OUTPUT a
   a←a+1 
ENDWHILE
# will output 1, 2, 3
20
Q

Definite (count controlled) iteration

FOR-TO-[STEP]- ENDFOR
If STEP IntExp is missing it is considered to be 1.

A

FOR Identifier ← IntExp TO IntExp [STEP IntExp]
# statements here
ENDFOR
# If STEP IntExp is omitted the step value is 1.

21
Q

FOR a ← 1 TO 3
OUTPUT a
ENDFOR

What will this output?

A

will output 1, 2, 3

22
Q

FOR a ← 1 TO 5 STEP 2
OUTPUT a
ENDFOR

What will this output?

A

will output 1, 3, 5

23
Q

FOR-IN-ENDFOR (repeat the statements the number of times that there are characters
in a string)

A

FOR Identifier IN StringExp
# statements here
ENDFOR

24
Q

Pseudocde to calculate the numbers of characters in message

A

length ← 0
FOR char IN message
length ←length +1
ENDFOR

# will calculate the
# number of characters
# in message
25
Q

Pseudocde to output a string in revere

A
reversed ← ''
FOR char IN message
   reversed ← char +
reversed
ENDFOR
OUTPUT reversed

will output the string in reverse

26
Q

SELECTION

IF-THEN-ELSE-ENDIF (execute the statements following the THEN if the Boolean expression is True, otherwise execute the statements following the ELSE)

A

IF BoolExp THEN
# statements here
ENDIF

a← 1
IF (a MOD 2) = 0 THEN
   OUTPUT ‘even’
ELSE
   OUTPUT ‘odd’
ENDIF
27
Q

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

A
IF BoolExp THEN
   # statements here
ELSE
     IF BoolExp THEN
          # statements here
     ELSE
         # statements here
     ENDIF 
ENDIF
28
Q

IF-THEN-ELSE IF ENDIF (removes the need for multiple indentation levels)

A
IF BoolExp THEN
   # statements here
ELSE IF BoolExp THEN
   # statements here
   # possibly more ELSE
ELSE
   # statements here
ENDIF
29
Q

ARRAYS ASSIGNMENT

A

Identifier ← [Exp, … ,Exp]

primes ← [2, 3, 5, 7, 11, 13]

30
Q

Arrays

Accessing an elemenT

A

Identifier[IntExp]

31
Q

primes ← [2, 3, 5, 7, 11, 13]

primes[0]

A

evaluates to 2
(questions on exam papers will start
indexing at 0 unless specifically stated
otherwise)

32
Q

Arrays

Updating an element

A

Identifier[IntExp] ← Exp

primes ← [2, 3, 5, 7, 11, 13]

primes[5] ← 17
# array is now [2, 3, 5, 7, 11, 17]
33
Q

Accessing an element in a two-dimensional array

A

Identifier[IntExp (row)][IntExp (column)]

34
Q

table ← [[1, 2],[2, 4],[3, 6],[4, 8]]

table[3][1]

A
# evaluates to 8 as second element 
# (with index 1) of fourth array 
# (with index 3) in table is 8
35
Q

Updating an element in a two- dimensional array

A

Identifier[IntExp][IntExp] ← Exp

36
Q

table ← [[1, 2],[2, 4],[3, 6],[4, 8]]

table[3][1] ← 16

A

table[3][1] ← 16

# table is now
#[ [1, 2],
#  [2, 4],
#  [3, 6],
# [4, 16] ]
37
Q

Array length

A

LEN(Identifier)

38
Q

table ← [[1, 2],[2, 4],[3, 6],[4, 8]]

LEN(table)

A

evaluates to 4

39
Q

table ← [[1, 2],[2, 4],[3, 6],[4, 8]]

LEN(table[0])

A

Evaluates to 2

40
Q

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

A

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
41
Q

Record declaration pseudo-code

A

RECORD Record_identifier
field1 :

field2 :

ENDRECORD

42
Q

Record for a car declaration: make, model, reg, price, noOfDoors

A
RECORD Car
make : String 
model : String
reg : String 
price : Real 
noOfDoors : Integer 
ENDRECORD
43
Q

Pseudo-code record variable initialisation

A

varName ←
Record_identifier(value1,

value2, …)

44
Q
RECORD Car
make : String 
model : String 
reg : String 
price : Real noOfDoors : Integer 

Initialise a ford: pseudo-code

A

myCar ← Car(‘Ford’, ‘Focus’, ‘DX17

GYT’, 1399.99, 5)

45
Q

Assigning a value to a field in a record - pseudo-code

A

varName.field ← Exp

46
Q

myCar ← Car(‘Ford’, ‘Focus’, ‘DX17
GYT’, 1399.99, 5)

Change model to fiesta, pseudo-code

A
myCar.model ←'Fiesta'
# The model field of the myCar 
# record is assigned the value 
# 'Fiesta'.
47
Q

Accessing values of fields within records - pseudocode

A

varName.field

48
Q

What does this do? (Records topic)

OUTPUT myCar.model

A

OUTPUT myCar.model

# Will output the value stored in 
the 
# model field of the myCar record
49
Q

Difference between subroutines and functions in pseudo-code

A

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.

50
Q

Subroutine definition

A
SUBROUTINE Identifier(parameters) 
# statements here
ENDSUBROUTINE
51
Q

Subroutine to add two numbers

A

SUBROUTINE showAdd(a, b)
result ← a + b
OUTPUT result
END SUBROUTINE

52
Q

SUBROUTINE sayHi()
OUTPUT ‘Hi’
ENDSUBROUTINE

Function or procedure?

A

Procedure

53
Q

SUBROUTINE add(a, b)
result ← a + b
RETURN result

Function or procedure?

A

Function

54
Q

Calling subroutines without a return value

A

Identifier(parameters)

showAdd(2, 3)

55
Q

Calling subroutines with a return value

A

identifier ← identifier(parameters)

answer ← add(2, 3)

56
Q

String length pseudo-code

A

LEN(StringExp)

57
Q

LEN(‘computer science’)

A

evaluates to 16(including space)

58
Q

Position of a character

A

POSITION(StringExp, CharExp)

59
Q

POSITION(‘computer science’, ‘m’)

A
# evaluates to 2 (as with arrays
# exam papers will start # indexing at 0 unless
# specifically stated
otherwise)
60
Q

Substring

A

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)

61
Q

SUBSTRING(2, 9, ‘computer science’)

A

evaluates to ‘mputer s’

62
Q

Concatenation

A

StringExp + StringExp

63
Q

‘computer’ + ‘science’

A

evaluates to ‘computerscience’

64
Q

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 -
A
STRING_TO_INT(StringExp)
   STRING_TO_REAL(StringExp)
   INT_TO_STRING(IntExp)
   REAL_TO_STRING(RealExp)
   CHAR_TO_CODE(CharExp)
CODE_TO_CHAR(IntExp)
65
Q

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)

A
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
66
Q

User input

A

USERINPUT

a ← USERINPUT

67
Q

Output

A

OUTPUT a

OUTPUT a, g

The output statement can be followed by multiple StringExp separated by commas

68
Q

Random integer generation

A

Random integer generation (between two integers inclusively)

Identifier ← RANDOM_INT(IntExp, IntExp)

69
Q

diceRoll ← RANDOM_INT(1, 6)

A

Will randomly generate an integer between 1 and 6 inclusive