4.2 Lesson 1 - Loops (45 Mins)
4.2 Lesson 1 - Loops (45 Mins)
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 1/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
Description
0:00 / 8:03
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 2/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
Table of contents
1. Introduction
2. Loops (I do)
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 3/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
1. Introduction
The loop is a component included in all programming languages. Loops are used to repeat certain statements a predetermined
number of times or according to a predefined condition. For, repeat... until, and while... do are only three of Delphi's control
structures that repeatedly run code blocks. In this lesson, you will discover how to use these structures to develop code.
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 4/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
2. Loops
Let's answer the following question before we get started.
What is the device that provides battery backup when the electrical power fails?
UPS
Fibre
RAID
AUP
Check
LoopVariable: Acts as a counter for the for loop. The loop variable must be an ordinal data type, such as an integer or
character.
StartValue: This is the first value assigned to the Loop variable.
EndValue: The LoopVariable will be increased by 1 when the loop is executed until the value of the loop variable is greater
than the end value. Then the loop will stop.
This is a for-loop pseudocode. It is not the code itself but rather a description of what it should do.
begin
<instruction(s) >
end;
Let's look at the algorithm of the for loop along with the flow chart below.
The loop structure below counts from a low to a high value. Each loop iteration increases the counter 'iCount' by one. If the
counter reaches one more than the maximum value, the iteration ends, and the statement after the loop is executed.
for icount:= 1 to 10 do
begin
<instruction(s) >
end,
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 5/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
The repeat loop is used when we don't know how many times the loop will be executed, but the loop should be executed at
least once.
The loop terminates once the condition True is met.
The repeat loop is a post-test loop, meaning that once the body of the loop has been executed, the condition will be tested.
The loop will 'crash' if all the known programming principles are not followed.
Do not insert a semi-colon after Repeat.
Begin and end statements are optional.
This is a repeat loop pseudocode. It is not the code itself, but rather a description of what it should do.
repeat
<instruction(s) >
Let's look at the ICT principle that holds for the repeat loop.
The ICT principle helps you find the balance between where the loop starts and ends.
Initialise Assign values to the variable(s) of the loop termination before the start.
The variable used in the loop termination condition needs to be changed inside the loop. Otherwise, the condition(s)
Change
will always be False, causing the loop to run infinitely.
The variable is tested in the loop-termination condition. If the condition is False, then the statement within the loop
Test
is executed.
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 6/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
The following Delphi code is an example of a repeat loop. This code adds numbers until the sum of the numbers is greater than
100.
iNumber := 1;
iTotal := 1;
repeat
inc(iNumber);
lblDisplay.Caption := IntToStr(iNumber)
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 7/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
This is a while loop pseudocode. It is not the code itself but rather a description of what it should do.
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 8/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
begin
<instruction(s)>
end;
The ITC principle helps you find the balance between where the loop starts and ends.
Initialise
The loop control variable must be assigned before the while statement evaluates the condition.
The variable is tested in the loop continuation condition. The statement within the loop is executed if the condition
Test
is true.
A statement inside the loop's body should change the value of the variable used in the loop continuation
Change
condition. Otherwise, the condition will always be true, resulting in an infinite loop.
The following Delphi code is an example of a while loop. The code displays all the multiples of 16 less than 100. When the
condition is True, an empty loop will be executed, and the program will return.
iNumber := 16;
while iNumber < 100 do
begin
memOutput.Lines.Add(IntToStr(iNumber));
iNumber := iNumber + 16;
end ;
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 9/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 10/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
This string takes up 12 spaces of text. If you want to refer to one of the characters, you can use square brackets:
In Delphi, we simply use the + operator to join strings together. For example: redOutput.Lines.Add(sName + sSurname);
You can use a function as well – concat(). For example: lblname.caption := concat(sName,’ ‘,sSurname);
3.1.2 Example 1
A for loop can count a specific letter that appears in a string several times. In this example, the user enters the string and the
letter into two edit points. The index of the for loop is used to step through the character of the string and compare each of
the characters in the string to the chosen letter. The final results can only be displayed once all the characters in a string are
compared to the letter. The following table shows the characters 'Welcome Piet' and its index value.
W e l c o m e P i e t
1 2 3 4 5 6 7 8 9 10 11 12
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 11/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
begin
iNumLetter := 0;
sSentence := edtSentence.Text;
cLetter := edtLetter.Text(1);
for iIndex := 1 to Length(sSentence) do
begin
if sSentence(iIndex) = cLetter then
Inc(iNumLetters);
end;
pnIOut.Caption := 'The letter ' + cLetter + 'appears' + IntToStr(iNumLetters) + 'times in the sentence ' +
sSentence;
end;
Happy New Year pretty lady – are you going to the party?
becomes
Hppy Nw Yr prtty ldy – r y gng 2 th prty tnght?
var
K :integer;
sSentence, sNewsS : string;
begin
sSentence := edtInput.Text;
sNewsS := '' ;
for K : = 1 to Length(sSentence) do
if NOT(Upcase(sSentence[K] IN ['A', 'E', 'I', 'O', 'U'] )
then sNewsS :=snewsS + sSentence [K];
edtSMS.Text := sNewS;
end;
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 12/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
4. Activity
Use the questions below to assess your understanding of loops. Review the material again if you spot any concepts that need
to be reviewed.
True False
Check
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 13/14
10/27/23, 1:51 AM 4.2 Lesson 1: Loops [45 mins]
Conclusion
In this lesson, you learnt that loops are used to repeat particular statements a certain number of times or based on a condition.
You learnt the various loops for programming: for, repeat and while. For loops repeat instructions a certain number of times.
The while loop is repeated until a given condition is met. Unlike the while loop, the repeat loop checks for a condition after
specific statements have been executed rather than before.
References
Noomé, C., Gibson, K., Macmillan, P., Wassermann, U. (2014). IT is gr8! @ Grade 11. Dorandia (Pretoria), South Africa: Study
opportunities.
Western Cape Education Department. (2019). Gr 11 Information Technology Practical Guide. Available at:
https://wcedeportal.co.za/eresource/88101 (Accessed: 26 August 2022).
https://campus.uctonlinehighschool.com/mod/book/tool/print/index.php?id=73521 14/14