Updated some Syntaxes to match the cambridge CS syntax
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)
-
-
Declaration
- Variable
1. DECLARE variableName: DataType
- Constant
1. CONSTANT constantName: DataType = Value
- Array
1. DECLARE arrayName: ARRAY[startIndex : endIndex] OF DataType
- 2D Array
1. DECLARE arrayName: ARRAY[startRowIndex : endRowIndex, startColumnIndex : endColumnIndex] OF DataType
- Variable
-
Input / Output
-
Input
1. INPUT variable
-
Output
1. OUTPUT variable
-
Note: you can input and output multiple times in 1 INPUT / OUTPUT
1. INPUT variable1 , variable2 ...
2. OUTPUT variable1 , variable2 ...
-
Input
-
If statement
-
Regular IF
-
1. IF condition
2. THEN
3. // code
3. ENDIF
-
-
IF with ELSE
-
1. IF condition
2. THEN
3. // code
4. ELSE
5. // code
6. ENDIF
-
-
IF with ELSE IF
-
1. IF condition
2. THEN
3. // code
4. ELSE
5. IF condition
6. THEN
7. // 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
- Declaration
-
1. PROCEDURE procedureName (parameter : DataType, ...)
2. // code
3. ENDPROCEDURE - Calling
-
1. Call procedureName // without parameters
2. Call procedureName (parameter, ...) // with parameters
-
Functions
- Declaration
-
1. FUNCTION functionName (parameter : DataType, ...) RETURNS DataType
2. // code
3. ENDFUNCTION - Calling
-
1. variable = functionName (parameter, ...) // sets the value returned from the function to the variable
-
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
Ideal Codes
-
Validating user's input
- Using WHILE
-
1. DECLARE inputVar: INTEGER
2.
3. INPUT inputVar
4.
5. WHILE inputVar < 0 OR inputVar <> INT(inputVar) DO
6. INPUT inputVar
7. ENDWHILE - Using REPEAT
-
1. DECLARE inputVar: INTEGER
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. DECLARE arrayLength: INTEGER
2. OUTPUT "Enter list length"
3. INPUT arrayLength
4.
5. // Declaring rest of variables
6. DECLARE list: ARRAY[1 : arrayLength] OF REAL
7. DECLARE maximum: INTEGER = -9999 // very low number (that will never be reached)
8. DECLARE minimum: INTEGER = 9999 // very high number (that will never be reached)
9. DECLARE total: INTEGER = 0
10. DECLARE num: INTEGER
11.
12. // Input the list array elements
13. FOR i = 1 TO arrayLength
14. OUTPUT "Enter list element number ", i
15. INPUT num
16. list[i] = num
17. NEXT i
18.
19. FOR i = 1 TO arrayLength
20. IF list[i] > maximum THEN
21. maximum = list[i]
22. ENDIF
23.
24. IF list[i] < minimum THEN
25. minimum = list[i]
26. ENDIF
27.
28. total = total + list[i]
29. NEXT i
30.
31. // outputting the results
32. OUTPUT "The maximum number is: ", maximum
33. OUTPUT "The minimum number is: ", minimum
34. OUTPUT "The total is: ", total
35. OUTPUT "The average is:", total / arrayLength
-
-
Bubble Sort for an array
-
1. DECLARE arrayLength: INTEGER
2. OUTPUT "Enter list length"
3. INPUT arrayLength
4.
5. // Declaring rest of variables
6. DECLARE list: ARRAY[1 : arrayLength] OF REAL
7. DECLARE num: INTEGER
8.
9. // Input the list array elements
10. FOR i = 1 TO arrayLength
11. OUTPUT "Enter list element number ", i
12. INPUT num
13. list[i] = num
14. NEXT i
15.
16. // Sorting the list ascending
17. DECLARE flag: BOOLEAN = true
18. WHILE flag = true DO
19. flag = false
20. FOR i = 2 TO arrayLength
21. // change the < to > if you want to sort descending
22. IF list[i] < list[i-1] THEN
23. DECLARE swap: INTEGER = list[i]
24. list[i] = list[i-1]
25. list[i - 1] = swap
26. flag = true
27. ENDIF
28. NEXT i
29. ENDWHILE
30.
31. // outputting the sorted list
32. OUTPUT "Sorted list:", i
33. FOR i = 1 TO arrayLength
34. OUTPUT list[i]
35. NEXT i
-
-
Liner search for an array
-
1. // Inputting the array length
2. DECLARE arrayLength: INTEGER
3. OUTPUT "Enter list length"
4. INPUT arrayLength
5.
6. // Declaring the rest of variables
7. DECLARE list: ARRAY[1 : arrayLength] OF REAL
8. DECLARE index: INTEGER = 1
9. DECLARE found: BOOL = False
10. DECLARE num: INTEGER
11.
12. // Input the list array elements
13. FOR i = 1 TO arrayLength
14. OUTPUT "Enter list element number ", i
15. INPUT num
16. list[i] = num
17. NEXT i
18.
19. // Input number to find
20. OUTPUT "Enter the number to find."
21. INPUT num
22.
23. // Looping to find the number
24. WHILE found = false AND index <= arrayLength DO
25. IF num = list[index] THEN
26. found = true
27. ELSE
28. index = index + 1
29. ENDIF
30. ENDWHILE
31.
32. // Outputting the result
33. IF found = true THEN
34. OUTPUT "The number is found and it's index is ", index
35. ELSE
36. OUTPUT "The number was not found"
37. ENDIF
-