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