Chapter 8 - Programming
Programming Constructs
Data Use (Variables, Constants and Arrays)
Sequence (Order in which steps are executed)
Selection (Choosing a path through a program)
Iteration (Repetition of sequence of steps)
Operators (Arithematic or Logical or Boolean)
Input/Output
Programs require I/O to process data
Prompt users properly with instructions
Inputs are strings
File Handling
Computer stores data in a file
Open File first : OPENFILE "filename.txt" FOR Operation
Always close file : CLOSEFILE "filename.txt"
Writing to a file : Operation WRITE
OPENFILE "file.txt" FOR WRITE
WRITEFILE "file.txt", Value
CLOSEFILE "file.txt"
Reading a file : Operation READ
OPENFILE "file.txt" FOR READ READFILE "file.txt", Value CLOSEFILE "file.txt"
Reading till EOF OPENFILE "file.txt" FOR READ WHILE NOT EOF("file.txt") DO
READFILE "file.txt", line
OUTPUT SUBSTRING(line, 1, 2)
ENDWHILE
Library Routines
Pre tested and available for use
Standard library of functions & procedures
MOD(Num1, Divident) => Remainder of Num1 divided by divident
DIV(Num1, Divident) => Quotient of Num1 divided by Divident
ROUND(Value, Dp) => Rounds value to x d.p 23.44444 23.4 ROUND(3.44444, 1)
RANDOM() => Returns a random float value between 1 and 0
- Calculate a random float value between Max and Min : (RANDOM() (Max - Min)) + Min
- Calculate a random integer value between Max and Min : ROUND((RANDOM() (Max -
Min)) + Min, 0)
Creating a maintanable program
Using meaningful identifier names
Divide each module into procedure and functions
Comment on your code
Texts in code which are not run
String Handling
String can store various characters
LENGTH(Str) - Returns the amount of characters in a string
UCASE(Str) - Returns the string in uppercase
LCASE(Str) - Returns the string in lowercase
SUBSTRING(Str, Start, Len) - Returns the part of string starting at position Start for Len
character.
SUBSTRING("Ateeb", 2, 3) -> tee
Arithematic Operators - + - / *
Logical Operators = > < >= <= <>
Boolean Operators NOT AND OR
Nested Statements
Code statements can be placed inside one another
They help reduce data duplication
Procedures & Functions
Procedure - A collection of programming statements without any return value that
accomplish a particular task.
PROCEDURE Name(Param: DataType)
[statements]
ENDPROCEDURE
PROCEDURE SayHelloNTimes(times: INTEGER)
FOR i <- 1 TO times
OUTPUT "hello"
NEXT i
ENDPROCEDURE
CALL SayHelloNTimes(5)
Functions - A collection of programming statements that return a value that is calculated
via accomplishing a particular task.
FUNCTION Name(Parm: DataType): RETURNS DataType
[statements]
ENDFUNCTION
FUNCTION CalcSum(Num1: INTEGER, Num2: INTEGER) RETURNS INTEGER
value <- Num1 + Num2
RETURN value
ENDFUNCTION
val <- CalcSum(1, 2)
Header - The first line
Local & Global Variables
Any part of a program can use a global variable
DECLARE value: DATATYPE AS GLOBAL
A local variable can only be used in the scope it's declared in
Arrays
A data structure containing several items of the same data type
[[1,2,3], [1,2,3], [1,2,3], [1,2,3]]
[
[1,2,3]
[1,2,3]
[1,2,3]
[1,2,3]
]
Value Value Value
1 2 3
1 2 3
1 2 3
1 2 3
Declaring a 2D Array
DECLARE Name: ARRAY [RowStart: RowEnd, ColumnStart: ColumnEnd] OF DataType
DECLARE Values : ARRAY [1: 4, 1:3] OF INTEGER
Values[2, 3] => 3
ArrayName[row, column]
Values[2, 3] <- 3
Filling up a 1D Array
Array of length 10 and you need to intiialise it with empty string
DECLARE Arr: ARRAY[1:10] OF STRING
FOR i <- TO 10
Arr[i] <- ""
NEXT i
Filling up a 2D Array
Array of length 10x12 and you need to intiialise it with empty string
DECLARE Arr: ARRAY[1:10, 1:12] OF STRING
FOR r <- TO 10
FOR c <- 1 TO 12
Arr[r, c] <- ""
NEXT c
NEXT r