It Grade 11 Notes
It Grade 11 Notes
● Arrays are volatile memory, meaning that they get stored in the ram and are deleted
shortly after
● The length function when looking at an array will give the total number of items stored in
the array, whereas the high function will return the value of the highest array
○ arrNames = ["Tim", "John", "Brian", "Sam", "Dricus", "Adam", "Steve"];
○ The length of arrNames is 7 as there are 7 total items
○ The high of arrNames is 6 as 6 is the highest index (Remember, arrays start at
0!)
● When declaring arrays, we declare them under var, just like how we declare other
variables
○ Var
○ arrPrizes: array [1..5] of string;
■ arr is the prefix for arrays
■ The numbers between the square brackets are the indexes. 1..5 means
the array has a total of 5 items with the index starting from 1 and ending
with 5 so that the first item is stored as item 1 and the last one is stored
as item 5
■ The ‘of’ section after the square brackets is used to declare the data type
of the arrays. Examples are strings, integers, reals ect.
● To assign a value to a certain item index within an array, you assign it under the ‘begin’
similar to how you assign values to other variable types
○ Begin
○ arrPrizes[1] := 'Playstation 5';
○ arrPrizes[2] := 'Vacation';
○ arrPrizes[3] := 'Gaming computer';
○ end
🔍EXTRA NOTE
Remember, when using the Random function, you should always use Randomize;
before it just to ensure that the numbers are truly random
● Randomize;
● iRandom:= Random(5);
● For the index of arrays, you don’t always have to use numbers. Letters work as well
○ arrPrizes : array [‘A’..’C’] of string
■ In this case you could declare values to the item indexes of the array the
same way as you do with numbers
○ arrPrizes[‘A’] := 'Playstation 5';
○ arrPrizes[‘B’] := 'Vacation';
○ arrPrizes[‘C’] := 'Gaming computer';
● Arrays can also be declared as constants. To do this you declare them right at the top of
the page under the private section
○ Const
○ arrMovies : array [1..8] of string = ('Shrek', 'Puss in Boots', 'Up', 'Toy Story',
'Frozen', 'Harry Potter', 'Encanto', 'Detective Pikachu');
■ This section is the same as when declaring a regular array as a variable
type
■ The difference between declaring an array as a variable and as a
constant is that when declaring it as a constant, you also add the values
of the indexes. You do this by using an equals sign and then writing every
item value in circle brackets and separated by commas. Note that due to
this array being a string type the items get quotation marks
● If you don’t know what the value of the lowest item index of an array is you can use the
low function. Alternatively if you don’t know what the highest value of the item index is,
you can use the high function
● These 2 functions can come in handy when using for loops
○ For i := low(arrMovies) to high(arrMovies) do
If you would like to exit a loop without exiting the entire code, don’t use the exit
function, use break instead.
● For i := 1 to 10
● Begin
● ShowMessage(‘Hello world’)
● Break;
● End;
🔍EXTRA NOTE
● You can also find the index chosen using ItemIndex and use a variable in the
square box
○ iIndex:= ListBox1.itemindex;
○ sFruit:= Listbox.items[iIndex];
🔍EXTRA NOTE
● The LoadFromFile function allows you to automatically load all the lines from a text file
○ memDisplay.Lines.LoadFromFile('Athletics.txt');
○ This will load all the lines from the text file called Athletics.txt
● When trying to save a text file as a variable, you use the TextFile type
○ tTemp : TextFile
● You can assign files to a variable but the variable has to be the right variable type
○ To assign files to a variable you use the AssignFile function
○ AssignFile(tNames,’Gamers.txt.’);
○ This is the variable that you are trying to save the file into
○ This is the name of the file you are trying to save as the variable.
○ NB! The name has to be in quotation marks and needs the .txt extension
🔍EXTRA NOTE
A very useful function one can use when writing code is a try block
A try block is similar to an if else statement but rather tries something and if it doesn’t work it
does something else
● Try
● (Your code);
● Except - This will originally be finally but you need to change it manually
● (Your code);
● end;
Under the try you need to write the code you originally want to output
Under the except you need to write the code you want to output if the code under your try
cannot output
● Before you can read from a file you need to open it
○ You can open a file with reset(file or variable name) to open the file without
changing any of the content
■ You cannot add lines to the file if you open it through this method
○ You can open a file with rewrite(file or variable name) to open a file and erase
all of the content on the file
■ This allows you to add lines from the first line to wherever you want as no
more content will be saved on the file
○ You can open a file with append(file or variable name) to open a file without
clearing any of the content on it
■ This allows you to add lines to the file after the last line
● The EOF function allows you to do code until you reach the end of a file
○ While NOT EOF (tTemp)
○ This is a loop which allows you to loop through the lines of a file until you reach
the end of it and do code every time
● The readln function allows you to read a line from a file into a variable
○ ReadLn(tTemp,sLine);
○ Variable your files saved into
○ Variable you’re trying to read/copy the specific line into
🔍EXTRA NOTE
● When sorting an array using the selection sort, you need 3 different variables
○ The first variable (i) is used to track the array from the lowest index to the highest
index minus 1
○ The second variable (j) tracks the array from the index of i+1 to the highest index
○ The third variable (iTemp) is used a temporary way to store a number
● You sort string variables in exactly the same way as you would integer values, but the
difference is that the temporary variable has to be a string type
Functions VS Procedures:
● Both Functions and Procedures fall under the broad category of methods
● Examples of methods include:
○ Pos
○ Copy
○ Delete
○ IntToStr
○ Inc
PROCEDURE FUNCTION
Delete Pos
Inc Copy
IntToStr
🔍EXTRA NOTE
You can output a built in sound in your code using the Windows.beep function
● Windows.beep(300,150);
● The first value is the frequency
● The second value is the duration in milliseconds
● You can create your own function by declaring function NAMEOFFunction under the
private section
○ Naming a function has the same rules as declaring variables
○ Once you have written function NAME under private, press CTRL + SHIFT + C to
create the skeleton code of the procedure
○ It's important to note that unlike procedures, you need to declare the variable
type of the function, such as string integer or real
● To assign a value that your function should return, you call on the result function
○ You can assign values to the result as you see fit
○
○ Here the results are Male if the seventh character of edtID is between 5 and 9,
and female if its between 0 and 4
○ You can then assign this result to a variable, or display it somewhere after you
have called on the function
🔍EXTRA NOTE
To load all the lines from a text file into a rich edit use:
● redOut.lines.loadfromfile(FileName);
Databases:
CONNECTING YOUR DATABASE
● A record is a row
● A field is a column
● Just like your form has a name, your data module also has a name
● In order to work with a database, you have to write the data modules name in the
uses block of your program
● When using a database, call the database by using with dmNAME do as this will allow
you to work with all the databases within your Data Module
● Similar to how when working with a text file, you need to set the cursor at the start by
using reset, you have to do the same with a database by using .first and you use .next
to go the next line
○
○
● Similar to the While Not EOF for text file, you can use While Not tblNAME.EOF to loop
through a table
○
● You can check a certain field within a table by using square brackets and the field name
inside the
○ This will display the contents of the field based on the specific line which is being
read on the table
○
○ Here we are checking if the contents within grade field equal edtGrade.text, and
if so we add the contents within the TourMoney field on the same line to our
rTotal
○
● When Inserting data into a database you first call on your data module
○ You then set your database to insert mode
○ You then equate each field to a value
○
○ NB!!! The field names have to be exactly the same as they appear in the table
○ NB!!! Be sure to double check the data type of the different fields of the
database, as the values you add have to match
○ You then use tblNAME.post to actually add the data to your tables
● If you would like to set the record number that the table is working on to a
specific one, use tbl_Name.RecNo:= Number
○ This will cause the cursor to be on the specific record number you have
entered
🔍EXTRA NOTE
● When working with databases in a test, the database will be connected using
code as opposed to components
● This means you don’t have to call on the data module to work with the specific
tables in the database but can just immediately work with it
Dynamic Objects:
● To create a dynamic object, you first have to declare the object as a global variable
○ When declaring the object as a variable, ensure you are using the right name
for the class according to delphi (TPanel,TButton, e.c.t)
● Once you have declared the dynamic object, you now need to actually create the
object
○ You can create a dynamic object by using the variable you declared earlier
○ The format used when creating a dynamic object is pnlGreet :=
TPanel.Create(frmCashRegister);
■ This is the variable you have declared earlier
■ This is the class type of the component you are creating, NOT the
variable name
■ This is the parent object. A parent object is the object the component you
are creating is placed in. The left and right values are determined by the
object and the size of the object
●
● After you have created your dynamic object you have to set the properties of the
object
○ Important Values:
■ Parent (Which component the object appears on)
■ Left (How far from the left of the parent component the object should be
placed)
■ Top (How far from the top of the parent component will the object be
placed)
■ Height (how high the object is)
■ Width (how wide the object is)
● Once you are done with your dynamic object you need to free it
○ Freeing a dynamic object is basically like destroying it
○ To free an object use the code: ObjectName.Free;
🔍EXTRA NOTE
● We can make use of a dynamic objects assigned value when checking if the
object exists or not
○ We do this by checking that the objects value is not assigned to nil
○ If Assigned(edtChildren) then
○ If Assigned(edtChildren) = nil then