Chapter 8 - Programming
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)
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
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]
]
Filling up a 1D Array