Iteration – Count controlled
FOR I = 0 to 7
PRINT(“Hello”)
NEXT i
This would print hello 8 times (0-7 inclusive).
Iteration – Condition controlled
WHILE answer != “computer”
answer = INPUT(“What is the password?”)
ENDWHILE While Loop
DO
Answer = INPUT(“What is the password?”)
UNTIL answer == “computer” Do Until Loop
Subroutines
FUNCTION triple(number)
RETURN number * 3
ENDFUNCTION
Called from main program
Y =triple(7)
PROCEDURE greeting(name)
PRINT(“hello” + name)
ENDPROCEDURE
Called from main program
greeting(“Hamish”) Procedure
Arrays / Lists
ARRAY names[5]
names[0] = “Ahmad”
names[1] = “Ben”
names[2] = “Catherine”
names[3] = “Dana”
names[4] = “Elijah”
PRINT(names[3])
To open a file to read you should use OPENREAD.
READLINE should be used to return a line of text from the file.
myFile = OPENREAD(“sample.txt”)
x = myFile.READLINE()
myFile.CLOSE()
will print out the contents of sample.txt
myFile = OPENREAD(“sample.txt”)
WHILE NOT myFile.ENDOFFILE()
PRINT(myFile.READLINE())
ENDWHILE
myFile.CLOSE()
To open a file to write to openWrite is used and writeLine to add a line of text to the file
myFile = OPENWRITE(“sample.txt”)
myFile.WRITELINE(“Hello World”)
myFile.CLOSE()
Methods and attributes
PUBLIC and PRIVATE
PRIVATE attempts = 3
PUBLIC PROCEDURE setAttempts(number)
attempts = number
ENDPROCEDURE
PRIVATE FUNCTION getAttempts()
RETURN attempts
END FUNCTION
Declaring a class
CLASS Pet
PRIVATE name
PUBLIC PROCEDURE NEW(givenName)
Name = givenName
ENDPROCEDURE
ENDCLASS
Inheritance
CLASS dog INHERITS Pet
PRIVATE breed
PUBLIC PROCEDURE NEW(givenName, givenBreed)
SUPER.NEW(givenName)
Breed = givenBreed
ENDPROCEDURE
ENDCLASS