Examples of the use of psuedocode as required by the QCAA Digital Solutions syllabus.
Syntax
-
Data types
-
STRING (a string of characters)
-
CHAR (a single character)
-
INTEGER (an integer number)
-
REAL (a real number can contain decimals)
-
BOOLEAN (a true or false)
-
-
VARIABLE DECLARATION
- Variable - initial value not known
1. DECLARE variableName - Variable - initial value not known (an alternative)
1. INITIALISE variableName - Variable - initial value is known
1. SET variableName = value - Constant - same as a variable, but value does not change throughout algorithm
1. CONSTANT constantName - Array - initally empty
1. DECLARE arrayName = [] - Array - loaded with starting values
1. DECLARE arrayName = [value, value, value]
- Variable - initial value not known
-
INPUT / OUTPUT
-
Input - variable value comes from the user
1. INPUT variable -
Output - output variable value
1. OUTPUT variable -
Display - display variable value
1. DISPLAY variable -
Print - print variable value
1. PRINT variable -
Note: you can input and output multiple times in 1 INPUT / OUTPUT statement
1. INPUT variable1 , variable2 ...
2. OUTPUT variable1 , variable2 ...
-
Input - variable value comes from the user
-
SELECTION
-
Regular IF
-
1. IF condition THEN
2. // code
3. ENDIF
-
-
IF...ELSE
-
1. IF condition THEN
2. // code
3. ELSE
4. // code
5. ENDIF
-
-
IF...ELSEIF...ELSE
-
1. IF condition THEN
2. // code
3. ELSEIF condition THEN
4. // code
5. ELSE
6. // code
7. ENDIF
-
-
NESTED SELECTION
-
1. IF condition THEN
2. // code
3. ELSE
4. IF condition THEN
5. // code
8. ELSE
9. // code
10. ENDIF
11. ENDIF
-
-
Regular IF
-
Case of
-
1. CASE variable OF
2. value1: statement
3. value2: statement
4. value3: statement
5. OTHERWISE: statement
6. ENDCASE
-
-
Iteration (Looping)
- using FOR
-
1. FOR i = 1 TO endValue
2. // code
3. NEXT i - using WHILE
-
1. WHILE condition DO
2. // code
3. ENDWHILE - using REPEAT
-
1. REPEAT
2. // code
3. UNTIL condition
-
Procedures & Functions
- Declaration - without parameters
-
1. BEGIN functionName
2. // code
3. END functionName - Declaration - with parameters
-
1. BEGIN functionName (parameter1, parameter2 ...)
2. // code
3. END functionName - Calling
-
1. Call functionName // without parameters
2. Call functionName (parameter1, parameter1, ...) // with parameters
-
Some existing functions
- MOD -> Returns the remainder of division
-
1. MOD(num1,num2) - DIV -> Returns the integer quotient of division
-
1. DIV(num1,num2) - INT -> Returns integer part of decimal / floor of number
-
1. INT(num1) - ROUND -> Returns a round up of a decimal number
-
1. ROUND(num, numOfDecimalPlaces) - RANDOM -> Returns a random decimal between 0 and 1 inclusive
-
1. RANDOM()
-
String manipulation
- LENGTH -> To find the string's length
-
1. LENGTH("My string") // returns 9 - SUBSTRING -> To extract a substring from a string
- -> The first parameter is the string (or string variable) and the second is the starting character index and the third parameter is the length to select from the starting character index
-
1. SUBSTRING("My string", 3, 6) // returns "string" - UCASE -> To convert to uppercase
-
1. UCASE("My string") // returns "MY STRING" - LCASE -> To convert to lowercase
-
1. LCASE("My string") // returns "my string"
-
File Management
- Declaration of needed variables
1. DECLARE file: STRING // takes the path of the file for example "file.txt"
2. DECLARE variable: STRING // this will take the values of each line read from the file- Read -> To read a file line by line
-
1. OPENFILE file FOR READ
2. READFILE file, variable
3. OUTPUT variable // you can output here though
4. CLOSEFILE file - Write -> To write on a file line by line
-
1. OPENFILE file FOR WRITE
2. WRITEFILE file, variable
3. OUTPUT variable // you can output here though
4. CLOSEFILE file
Pseudocode Examples
-
Validating user's input
- Using WHILE
-
1. INPUT inputVar
2.
3. WHILE inputVar < 0 OR inputVar <> INT(inputVar)
4. INPUT inputVar
5. ENDWHILE - Using REPEAT
-
1. DECLARE inputVar
2.
3. REPEAT
4. INPUT inputVar
5. UNTIL inputVar > 0 AND inputVar = INT(inputVar)
For this example I am validating if the input is a positive integer
-
Finding maximum, minimum, total and average of an array
-
1. INPUT arrayLength
2.
3. // Declaring rest of variables
4. DECLARE listName[element0, element1, ... ]
5
6 // Input the list array elements
7 FOR i = 1 TO arrayLength
8 OUTPUT "Enter list element number ", i
9 INPUT num
10. list[i] = num
11. NEXT i
12.
13. FOR i = 1 TO arrayLength
14. IF listName[i] > maximum THEN
15. maximum = listName[i]
16. ENDIF
17.
18. IF listName[i] < minimum THEN
19. minimum = listName[i]
20. ENDIF
21.
22. total = total + list[i]
23. NEXT i
24.
25. // outputting the results
26. OUTPUT "The maximum number is: ", maximum
27. OUTPUT "The minimum number is: ", minimum
28. OUTPUT "The total is: ", total
29. OUTPUT "The average is:", total / arrayLength
-