selection: the next instruction to be executed depends on a 'condition'
condition: a logical expression that evaluates to true or false
iteration: a number of instructions are repeated
Modularisation: used for reducing the complexity of a system by deconstructing into more or less independent units or modules. (this is definition from syllabus. in addition modularisation assists with refactoring code, efficiency, scalability, and other systems criteria as well. 'modules' can be referred to as functions, methods, procedures, routines, or these words prefixed with sub- ...)
1. What is output?
BEGIN SET a = CALL b OUTPUT a END
BEGIN b RETURN 1 END b
2. What is exact output?
BEGIN SET max, avg = newSum(2,4) OUTPUT avg, max END
BEGIN newSum(num1,num2)
SET tempAvg = (num1 + num2) / 2
IF num1 > num2 THEN
RETURN num1, tempAvg
ELSE
RETURN num2, tempAvg
ENDIF
END newSum
3. What is output?
BEGIN GLOBAL VARIABLE x LOCAL VARIABLE y x = 1 y = 2 change() PRINT(x,y) END
BEGIN change x = 3 y = 4 END change
4. What is output? Is a 'local' or 'global' in swap() function?
BEGIN GLOBAL VARIABLE x LOCAL VARIABLE y x = 1 y = 2 y = swap(y) PRINT(x,y) END
BEGIN swap(a) temp = x x = a RETURN temp END swap
5. What is output?
BEGIN
a = 1
b = 2
z = go(a,b)
IF z == 1 THEN
OUTPUT 2
ELSE
OUTPUT 1
ENDIF
END
BEGIN go(x,y)
SET c = go2(y,x)
IF c == 1 THEN
RETURN 2
ELSE
RETURN 1
ENDIF
END go
BEGIN go2(x,y) RETURN y END go2
6. How modularisation could be beneficial in your IA's:
BEGIN
GLOBAL VARIABLE session #lasts until user closes browser
GLOBAL VARIABLE username, cartItems
IF session NOT(EXISTS) THEN
INPUT username, password
WHILE authenticate(username,password) <> 200
INPUT username, password
ENDWHILE #user authenticated
SIGN session WITH username, cartItems=[] #stores between pages
ENDIF #session established
WHILE session EXISTS: #user is browsing site:
INPUT url #from querystring
IF url == "/browse" THEN
displayItems() #yet to implement
ELSE
IF url == "/buy?itemID=x" THEN
READ itemID FROM url querystring parameter x
APPEND itemID TO cartItems
REDIRECT url TO "/browse"
ELSE #"/logout", respond to end user event:
DELETE session
ENDIF
ENDIF
ENDWHILE
END
BEGIN displayItems() ... pseudocode here ... END displayItems
BEGIN authenticate(u,p)
IF u <> "admin" OR p <> "1234" THEN
RETURN HTTP 403 FORBIDDEN
ELSE
RETURN HTTP 200 OK
ENDIF
END authenticate