Condition: a logical expression that evaluates to TRUE or FALSE.
Iteration: a repetition of instruction(s). Can be controlled by a test 'condition' in a loop(ing) construct.
Pretest loop: a pretest loop tests its condition before each iteration. A pretest loop may execute zero or more times.
Posttest loop: a posttest loop tests its condition after each iteration. A posttest loop will always execute at least once.
div(): Python function known as 'integer' or 'floor' division, returns the largest possible integer e.g. 5 div 2 == 2.
mod(): Python function known as 'Modulo Operator', returns the remainder of the division e.g. 5 mod 2 == 1.
Array: An array (Python known as list) can store a collection of data under a single variable name. Elements of an array can be numbered with an index between 0 and len(array)-1 where len (short for length) is total number of elements in array.
1. What is output? Is this pretest or posttest loop?
BEGIN
SET x = 0
REPEAT
OUTPUT x
SET x = x + 1
UNTIL x == 5
END
2. What is output? Is this pretest or posttest loop?
BEGIN
SET y = 0
WHILE y < 5
OUTPUT y
SET y = y + 1
ENDWHILE
END
3. What is output?
BEGIN
SET z = 21
WHILE z >= 3
IF z mod 3 == 0 THEN
OUTPUT z
ENDIF
SET z = z - 1
ENDWHILE
END
4. What is output?
BEGIN
SET a = 1
REPEAT
IF a mod 2 == 0 THEN
PRINT a div 2
ENDIF
SET a = a + 1
UNTIL a > 4
END
5. What is output?
BEGIN
SET m = 1
SET n = 3
WHILE m <= 3
WHILE n >= 1
IF m == n THEN
OUTPUT m
ELSE
OUTPUT n
ENDIF
n = n - 1
ENDWHILE
m = m + 1
ENDWHILE
END
6. What is output?
BEGIN
SET plainText = ['h','i']
FOR counter = 0 TO len(plainText) #'for' loops in Python won't execute end value
OUTPUT plainText[counter]
NEXT counter
END
7. What is output?
BEGIN
SET plainText = ['h','i']
FOR counter = 0 TO len(plainText) #'for' loops in Python won't execute end value
SET letter = plainText[counter]
SET number = ord(letter)
SET number = number + 1
OUTPUT chr(number)
NEXT counter
END
8. What is value of cipherPin at the end of this algorithm?
BEGIN
SET pin = [1,2,3,4]
SET cipherPin = []
SET shift = -3
FOR counter = 0 TO len(pin) #'for' loops in Python won't execute end value
SET cipherPin[counter] = pin[counter] + shift
NEXT counter
END