Comments
Comment
//
print(“Hello World”) // This is a comment
Variables
Constants
const
const vat = 0.2
Global Variables
global
global userID = "Cust001"
Input/Output
Input
input()
myName = input("Please enter a name")
Output
print()
print("My name is Noni")
Casting
Converting to another data type
str()
str(345)
Iteration
FOR loop
for ... to ... next ...
for i = 0 to 9
print("Loop")
next i
// This will print the word “Loop” 10 times, i.e. 0-9 inclusive.
for ... to ... step ...
next ...
for i = 2 to 10 step 2
print(i)
next i
// This will print the even numbers from 2 to 10 inclusive.
for i = 10 to 0 step -1
print(i)
next i
// This will print the numbers from 10 to 0 inclusive, i.e. 10, 9, 8, ..., 2, 1, 0.
// Note that the 'step' command can be used to increment or decrement the loop by any positive or negative integer value.
WHILE loop
while ...
endwhile
while answer != "Correct"
answer = input("New answer")
endwhile
// Will loop until the user inputs the string “Correct”. Check condition is carried out before entering loop.
DO UNTIL loop
do
until
do
answer = input("New answer")
until answer == "Correct"
// Will loop until the user inputs the string “Correct”. Loop iterates once before a check is carried out.
Condition
IF-THEN-ELSE
if ... then
elseif ... then
else
endif
if answer == "Yes" then
print("Correct")
elseif answer == "No" then
print("Wrong")
else
print("Error")
endif
SWITCH
switch … :
case … :
case … :
default:
endswitch
switch day :
case "Sat":
print("Saturday")
case "Sun":
print("Sunday")
default:
print("Weekday")
endswitch
String handling
String length
.length
subject = "ComputerScience"
subject.length // returns the value 15
Substrings
.substring(x , i)
.left(i)
.right(i)
subject.substring(3,5) // returns "puter"
subject.left(4) // returns "Comp"
subject.right(3) // returns "nce"
Concatenation
+
name = "Tom"
print("Your name is: " + name)
Uppercase
.upper
subject = "ComputerScience"
subject.upper // returns "COMPUTERSCIENCE"
Lowercase
.lower
subject.lower // returns "computerscience"
ASCII Conversion
ASC(...)
ASC('C') // returns 65
CHR(...)
CHR(97) // returns 'a'
File Handling
Open
open(...)
myFile = open("sample.txt")
// Note that the file needs to be stored as a variable.
Close
.close()
myFile.close()
Read line
.readLine()
myFile.readLine()
Write line
.writeLine(...)
myFile.writeLine("Add new line")
// Note that the line will be written to the END of the file.
End of File
.endOfFile()
while NOT myFile.endOfFile()
print(myFile.readLine())
endwhile
Create a new file
newFile(...)
newFile("myText.txt")
// Creates a new text file called "myText". The file would then need to be opened using the above command for Open.
Arrays
Declaration
array colours[...]
array colours[5]
// Creates 1D array with 5 elements (index 0 to 4).
array colours = ["Blue", "Pink", "Green", "Yellow", "Red"]
// Arrays can be declared with values assigned.
array gameboard[..., ...] = ...
array gameboard[8,8]
// Creates 2D array with 8 elements (index 0 to 7).
Assignment
names[...] = ...
names[3] = "Noni"
gameboard[..., ...] = ...
gameboard[1, 0] = "Pawn"
Sub programs
Procedure
procedure name(...)
endprocedure
procedure multiply(num1, num2)
print(num1 * num2)
endprocedure
Calling a procedure
procedure(parameters)
multiply(5, 7)
Function
function name(...)
endfunction
function squared(number)
squared = number^2
return squared
endfunction
Calling a function
function(parameters)
squared(4)
newValue = squared(4)
// Function returns should be stored in a variable if needed for later use in a program.
Random numbers
Random numbers
random(..., ...)
myVariable = random(1,6)
// Creates a random integer between 1 and 6 inclusive.