0% found this document useful (0 votes)
55 views35 pages

IT CHEAT Sheet 2024

The document is an IT cheat sheet for 2024 that covers various programming concepts, including file operations, loops, string manipulation, and database management. It provides examples of reading from and writing to text files, sorting data, deleting and inserting items, and handling duplicates. Additionally, it includes procedures and functions with parameters, as well as database navigation and primary key management.

Uploaded by

Dane Smit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views35 pages

IT CHEAT Sheet 2024

The document is an IT cheat sheet for 2024 that covers various programming concepts, including file operations, loops, string manipulation, and database management. It provides examples of reading from and writing to text files, sorting data, deleting and inserting items, and handling duplicates. Additionally, it includes procedures and functions with parameters, as well as database navigation and primary key management.

Uploaded by

Dane Smit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

IT CHEAT SHEET 2024

Table of Contents
Read From File ....................................................................................................................... 2
Writing to a Text File:............................................................................................................... 3
Highest/ Lowest of thing: ........................................................................................................ 4
Sorting from Smallest to Biggest ............................................................................................. 5
Find Many := FOR-LOOP ......................................................................................................... 6
Find ONE:= WHILE LOOP ........................................................................................................ 7
Example delete one item: ....................................................................................................... 8
Example to delete many names as per user input: ................................................................... 9
Example inserting at a certain position: ................................................................................. 10
Removing Duplicates: .......................................................................................................... 11
Swap Values of Two Variables ............................................................................................... 12
Count Number of instances.................................................................................................. 13
Formulas: ............................................................................................................................ 14
Find Even/ Odd .................................................................................................................... 15
Determine factors of a number ............................................................................................. 16
Reverse a string:................................................................................................................... 17
Isolate digits in a string: ........................................................................................................ 17
Finding Vowels : ................................................................................................................... 18
Replace characters in a String: ............................................................................................. 19
Procedure with Parametres : ................................................................................................. 20
Function with parameters:.................................................................................................... 20
DataBases: .......................................................................................................................... 24
Navigation ........................................................................................................................... 24
Find One in DB: .................................................................................................................... 26
No Duplicates ...................................................................................................................... 27
Duplicates ........................................................................................................................... 28
Insert into DB ....................................................................................................................... 29
Edit ONE record: .................................................................................................................. 30
Edit MANY records: .............................................................................................................. 31
Delete ONE Record: ............................................................................................................. 32
Delete MANY Records: ......................................................................................................... 33
Multiple Tables:.................................................................................................................... 34

1|Pa ge
Read From File
Var

tFile: TextFile;

sLine : String ;

iPos : Integer ;

begin Read from File NB!:


if FileExists('Shopping.txt') <> true then 1. FileExists
Begin 2. AssignFile(tfile,
Showmessage('File does not exist'); ‘Shops.txt’)
End; 3. Reset(tFile)
4. While Not
EOF(tFile) &
AssignFile(tFile, 'Shopping.txt.');
(iCount < 30)
Reset(tFile);
5. ReadLn(tfile,sLine)
iCount := 0;
6. CloseFile(tFile)

while not EOF(tFile) and (iCount < 30) do

Begin

Readln(tFile, sLine);

iCount := iCount + 1 ;

//OREO#1#4.99

iPos := Pos('#', sLine) ;

arrName[iCount] := Copy(sLine,1,iPos-1) ;

Delete(sLine,1,iPos) ;

iPos := Pos('#', sLine) ;

arrNumber[iCount] := StrToInt(Copy(sLine,1,iPos-1)) ;

Delete(sLine,1,iPos) ;

arrAmount[iCount] := StrToInt(Copy(sLine,1,iPos-1)) ;

End;

CloseFile(tFile) ;

2|Pa ge
Writing to a Text File:

Need To test if space


If iCount = 30 then

Begin

Showmessage(‘No more space’) ;

Exit;

End;

Inc(iCount) ;

arrShop[iCount] := Inputbox(‘Shop,’Enter name’,’’) ;

arrCustomers[iCount] := RandomRange(10,100) ; Get value from user


arrProfit[iCount] := StrToFloat(edtInput.Text) ;

AssignFile(tFile,’Shops.txt’);

If FileExists(‘Shops.txt’) <> true then

Rewrite(tFile) //Create the file

Else

Append(tfile) //Open the file to add to end

WriteLn(tFile,arrShop[iCount],’#’,arrCustomers[iCount],’*’,FloatToStrF(arrProfit[iCount],
ffFixed,10,2)) ;

Writing to text File NB:


1. Test for Space
2. Inc(iCount)
3. AssignFile(tFile,’..txt’)
4. FileExists(‘txt’) <> true then
a. Rewrite(tFile)
else
b. Append(tFile)
5. WriteLn(tFile,……)

3|Pa ge
Highest/ Lowest of thing:

Var
I, iHigh, iLow: Integer;
begin

iLow := 1;
iHigh := 1;

for I := 2 to iCount do
Begin
if arrAmount[I] < arrAmount[iLow] then
iLow := I ;

if arrAmount[I] > arrAmount[iHigh] then


iHigh := I ;
End;

redOutput.Lines.Add('Most Expensive was


‘+'+Uppercase(arrName[iHigh])+'@'+FloatToStrF(arrAmount[iHigh],ffCurrency,10,2)) ;
redOutput.Lines.Add('Least Expensive was’+
'+Uppercase(arrName[iLow])+'@'+FloatToStrF(arrAmount[iLow],ffCurrency,10,2)) ;

4|Pa ge
Sorting from Smallest to Biggest

For iOut := 1 to iCount - 1 do


For iIn := iOut + 1 to Count do
If arrCustomers[iIn] < arrCustomers[iOut] then
Begin
sKeep := arrShopliOut];
arrShop[iOut] arrShop|iIn];
arrShop[iIn] := sKeep;

iKeep := arrCustomers[iIn];
arrCustomers[iIn]:= arrCustomers[iOut];
arrCustomers[iOut] :=iKeep;

rKeep := arrProfit[iOut];
arrProfit[iOut] := arrProfit[iIn];
arrProfit[iIn] := rKeep;
End;

For k := 1 to iCount do
Begin
redOutput.Lines.Add(‘Highest’+ IntToStr(arrCustomers[k]) ;
End ;

Use normal display to


display it

5|Pa ge
Find Many := FOR-LOOP
Example: Search for a name that could appears many times in the array:

bFind := false;

for k:= 1 to Count do


if Uppercase(sSearch) = Uppercase(arrName[k]) then
begin
bFind := true;
redOutput lines.Add(arrName[k] + #9+ arrSurname[k]);
end;//if
Many you display the
output IN the loop

if bFind <> true then


Begin
redOutput lines.Add(sSearch +' was not found');
End;

6|Pa ge
Find ONE:= WHILE LOOP
Example: Search for the first occurrence in the array of the province entered:

bFind := false;
k:= 0;

while (k <iCount) and (bFind <> true) do


begin
inc(k);
if Uppercase(sSearch) = Uppercase(arrProvince[k]) then
bFind := true;
end;/While

if bFind <> true then


redOutput.lines.Add(sSearch+' was not found')
else
redOutput.lines.Add(arrProvince[k]+#9+arrPremier[k]+#9+IntToStr(arrCitie
s[k])) ;

One you display the output


OUTSIDE the loop

7|Pa ge
Example delete one item:
Search for the item making the loop stop once found.
Search := edtName. Text;
bFind:= false;
iDeletePos:= 0;

while (DeletePos < iCount) and (bFind <> true) do begin


inc(iDeletePos);
if Uppercase(sSearch) = Uppercase(arrName[iDeletePos]) then
bFind := true;
end;//while One Item ends the first while

Outside the loop, test if the item was found and delete if it was.
if bFind <> true then
redOutput.lines.Add(sSearch+' was not found.')
else
begin
for k:= iDeletePos to iCount -1 do begin
arrName[k]:= arName[k +1] ;
arrSurname[k]:= arrSurname[k +1];
arrMark[k] := arrMark[k 1] ;
arrGrade[k]:= arrGrade|k +1] ; arrName[iCount+1] := ‘’ ;

end; arrSurname[iCount+1} := ‘’ ;
arrMark[iCount + 1] := 0 ;

dec(iCount) ; arrGrade[iCount = 1] := 0 ;

Gets ride of duplicate


value at end
8|Pa ge
Example to delete many names as per user input:
Search := InputBox('Name', 'Enter the name you would like to search
for',’John');
bFind := false;
iDeletePos:= 0;

while iDeletePos < iCount do


begin
Inc(iDeletePos);
if Uppercase(sSearch) = Uppercase(arrName[iDeletePos]) then
begin
bFind:= true;
for k:= iDeletePos to iCount - 1 do
begin
arrName[k] := arrName[k +1];
arrSurname[k] := arrSurname[k +1];
arrMark[k] := arrMark[k +1];
arrGrade[k] := arrGrade[k +1];
end; //for
Dec(iDeletePos);
Dec(iCount);
end;

9|Pa ge
Example inserting at a certain position:

iPos:= StrTolnt(edtinput.Text);

for k:= iCount Down To iPos do begin


arrName[k +1]:= arrName[k];
arrSurname|k +1]:= arrSurname|k|;
arrMark[k +1] := arrMark[k];
arr Grade[k +1] := arr Grade[k] ;
end;

arrName[iPos] := sName;
arrSurname[iPos] := sSurname;
arrMark[iPos] := iMark;
arrGrade[iPos] := iGrade;
inc(iCount);

10 | P a g e
Removing Duplicates:

iDup := 0;

for k:= 1 to iCount do

begin

bDuplicates := false;

for iCheck := 1 to iDup do

if UpperCase(arrNewName[iCheck]) = UpperCase(arrName[k]) then

bDuplicates := True;

if NOT bDuplicates then begin

Inc(iDup);

arrNewName[iDup] := arrName[k];

arrNewSurname[iDup] := arrSurname[k];

arrNewMark[iDup] := arrMark[k];

arrNewGrade[iDup] := arrGrade[k];

end;

end;

Overwrite the original array if needed / asked to do so:

iCount:= iDup;

for k:= 1 to iCount do

begin

arrName[k] := arrNewName[k]; Only exist in RAM


arrSurname[k] := arrNewSurname|k]; need to Rewrite and
arrMark[k] := arrNewMark[k]; WriteLn to add to
arrGrade[k] := arrNewGrade[k]; tFile

11 | P a g e
Swap Values of Two Variables

rKeep := rValue1 ;
rValue1 := rValue2 ;
rValue2 := rKeep

12 | P a g e
Count Number of instances

arrSearch : array of String ;

function Count Instances


var
k, iCount: Integer;
sSearch : String ;
Begin

sSearch := edtSearch.Text ;
iCount := 0;

for k := 1 to 100 do
if arrSearch[k] = sSearch then
Inc(Count);
Result := iCount;
end;

13 | P a g e
Formulas:

iNum := 20 MOD 2 ; → 0 MOD→ Remainder

iNum := 20 DIV 2 ; → 10
DIV→ Integer

14 | P a g e
Find Even/ Odd
Begin

If rNumber mod 2 = 0 then


sResult := “Even”
else
sResult := “Odd” ;
end;

15 | P a g e
Determine factors of a number

var
iNum, i: Integer;
begin
iNum := 12;

//Factors

for I := 1 to iNum do
begin
if number mod I = 0 then
iFactor := I ;
end;

16 | P a g e
Reverse a string:

var
soriginal, sreversed: String;
i: Integer;
begin
soriginal := 'Delphi';
sreversed := '';

for i := Length(original) downto 1 do


sreversed := sreversed + soriginal[i];
end;

Isolate digits in a string:

var
s: String;
i: Integer;
begin
s := 'a1b2c3';

for i := 1 to Length(s) do
if s[i] in ['0'..'9'] then
sNew := sNew + s[i] ;
end;

17 | P a g e
Finding Vowels :
var
s: String;
i: Integer;
begin
s := 'Delphi Programming';

for i := 1 to Length(s) do
if s[i] in ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'] then
sNew := sNew + s[i] ;

end;

18 | P a g e
Replace characters in a String:

var
s: String;
i: Integer;
begin

s := 'Delphi Programming';

for i := 1 to Length(s) do
if s[i] = 'P' then
s[i] := 'B';
end;

19 | P a g e
Procedure with Parametres :
Procedure DON’T use Result

procedure Greet(name: String);


Function USE Result
begin
WriteLn('Hello, ', name);
end;

begin
Greet('Alice');
end.

Function with parameters:

function Add(a, b: Integer): Integer;


begin Result
Result := a + b; equals the
datatype
end;

begin
WriteLn('Sum: ', Add(5, 10));
end.

Function Add(a,b : Integer) : Integer ;

(Receive) (Return)

20 | P a g e
21 | P a g e
.

22 | P a g e
23 | P a g e
DataBases:

Navigation
Code can be used to move the file pointer to another active record:

Code Description
tblLearners.First; To move the pointer to the first record
tblLearners.Prior; To move the pointer to the previous record
tblLearners.Next; To move the pointer to the next record
tblLearners.Last; To move the pointer to the last record
tblLearners.RecNo:= iNum; To move the record pointer to a specific record
number indicated by an Integer number

Code Description
tblLearners.RecordCount Holds the number of active records in the
table
If tblLearner[‘Name’] = NULL then To find values that were not complete // Empty

Code Description
tblLearners.Sort := ‘Surname’ Ascending Sort
tblLearners.Sort := ‘Surname DESC’ Descending Sort
Two Fields Sort
tblLearners.Sort := ‘Surname, Name DESC’ Descending sort in TWO fields

24 | P a g e
Primary Keys:

If the primary key in the database is an AutoNumber datatype there will be no code to add the
number, since the next number will automatically be insert.

Some question could have the current Primary Key as an Integer number that is the same as the
number of records in the table. In this instance use RecordCount +1 to add a unique number.

THEREFORE

Primary Key = AUTONUMBER

No Code

Primary Key = Integer Number

Use

RecordCount +1

Eg.

tbILearner['LearnerlD'] := tblLearner.RecordCount + 1;

Loop Through Tables:


tblLearner.First ;

While not tblLearner.EOF do

Begin

tblLearner.Next;

end;

25 | P a g e
Find One in DB:
Example: Find one and make the loop stop once found

sName := ……(searching)

iGrade:= …..(searching)

bFind := False;

tbILearner. First;

While not tbILearner.Eof and (bFind <> True) do begin

if (tbILearner['Name'] = sName) and (tblLearner['Grade'] = iGrade ) then

begin

bFind := True;

redOutput.Lines.Add(tbILearner['Name'] +' '+ IntToStrtbILearner[‘Mark']));

end;//if

tblLearner.Next;

end;//While

if not bFind then

ShowMessage(sName+' in ' +IntToStriGrade) +' has not been found')

26 | P a g e
No Duplicates
Populate ComboBox / radiogroup / ListBox

tbITeacher.First;

while not tblTeacher.Eof do

begin

rgpTeacher.Items.Add(tb|Teacher [ID];

tblTeacher Next;

end;

rgp Teacher. Caption := 'Select a teacher ID';

27 | P a g e
Duplicates
Populate ComboBox / radiogroup / ListBox

tbILearner. First;

while not tblLearner.Eof do

begin

if cmbRegClass.Items.IndexOf(tblLearner ['RegClass']) < 0 then

cmbRegClass.Items.Add(tbILearner ['RegClass']);

tbILearner.Next;

end;

cmbRegClass.Text := 'Select a register class';

28 | P a g e
Insert into DB

With dmUser do

tblLearner.Insert;

tbILearner['LearnerlD'] := tblLearner.RecordCount + 1;

tbILearner['TeacherlD']:= tb|Teacher['ID'];

tbILearner['Name'] := 'John';

tbILearner['Surname']:= edtSurname.Text;

tbILearner|'Grade']:= 11;

tb|Learner['Mark'] := iMark;

tb|Learner['Paid'] := True;

tblLearner['Connected] := chkConnected.Checked;

tb|Learner['Date Registered'] := Date;

tbILearner.Post;

29 | P a g e
Edit ONE record:
Example: Search for and change one record.

bFind := false;

tbILearner.First;

while not tblLearner.Eof and (bFind <> True) do begin

if (tbILearner ['Name'] = sName) and (tblLearner ['Grade'] = Grade ) then

begin

bFind := True;

tblLearner.Edit;

tb|Learner['Mark']:= tblLearner['Mark'] + 10;

tblLearner.Post;

end; /if

tblLearner.Next; • Use the users input to determine which record


end; //while
needs to be changed
• Find the record and make the loop stop once
found
• .Edit and .Post must be placed inside the if
statement

30 | P a g e
Edit MANY records:
Example: Changing multiple records.

tblLearner. First;

while not tblLearner.Eof do begin

If tblLearner ['Grade'] = 11 then

begin

tolLearner. Edit;

tolLearner["Mark']:= tblLearner['Mark'] + 10;

tblLearner.Post;

end; /if

tblLearner.Next; //NB!! Outside the If but inside the While end; //while

end; //While

• Use the user's input to determine which


records need to change.
• Place the .Edit and .Post inside the While
loop.

31 | P a g e
Delete ONE Record:
Example: Delete one record and stop the loop once the record was found.

bFind := false;

tbILearner.First;

while not tbILearner.Eof and (bFind <> True) do

begin

if (tbILearner ['Name'] = sName) and (tblLearner ['Grade'] = iGrade ) then

begin

tblLearner. Delete;

bFind := True;

end //f

else

tbILearner.Next;

end; /While

• Use the user's input to determine which record needs


to be deleted.
• Search for the record and make the loop stop once
found, using a Boolean flag variable.
• Delete the record.
• Place the Next in the ELSE of the If-statement.

32 | P a g e
Delete MANY Records:
Example: Delete all the grade 12 learners' records.

iCount := 0; TIP:
tbILearner. First; If you have to click on the event
while not tblLearner. Eof do more than once to remove all
begin
the data you are searching for,
you probably did not placed
If tbILearner ['Grade] = 12 then
the Next in the else.
Begin

tbILearner.Delete;

inc(iCount);

end//If else

tblLearner.Next; //NB!! In the else

end;/While

if iCount <> 0 then

ShowMessage(IntToStriCount) + ' grade 12 learners were deleted')

Else

ShowMessage('No grade 12 learners were found');

• Use a While loop to search for the records to be deleted, without


making the loop stop once a record was found that met the condition.
• Place the .Next in the else of the If-statement. If a record is deleted,
the active record will move to the next record, since the current one is
no longer there. It is for this reason that the Next is placed in the else, so
records are not skipped once one was deleted.

33 | P a g e
Multiple Tables:
Example: Display the learner and teacher names for all grade 11's.

tblUsername.First;

while not tblUsername.Eof do

begin

if tblUsername['Grade'] = 11 then

begin

tblTeacher. First;

while not tblTeacher.Eof do

begin

if tblUsername['TeacherlD'] = tblTeacher['ID'] then //Find the linked field

redOutput.Lines.Add(tblUsername['Names'] +' '+ tblTeacher|'Teacher']);

tbITeacher. Next;

end;//while tblTeacher

end;// if Grade

tblUsername.Next;

end;//while tblUsername

34 | P a g e

You might also like