0% found this document useful (0 votes)
13 views

4.2 Lesson 1 - Loops (45 Mins)

Uploaded by

jmabafokengp2
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)
13 views

4.2 Lesson 1 - Loops (45 Mins)

Uploaded by

jmabafokengp2
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/ 14

10/27/23, 1:51 AM 4.

2 Lesson 1: Loops [45 mins]

4.2 Lesson 1: Loops [45 mins]


Site: UCT Online High School Printed by: Bafokeng Phahlang
Course: G11 Information Technology 2023-01 Date: Friday, 27 October 2023, 1:53 AM
Book: 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

Listen to this lesson


Click on the three dots on the audio block to download.

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)

3. Working with Loops (We do)

4. Activity (You do)

5. Conclusion and References

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.

By the end of this lesson, you will be able to:

Recall the use of for, repeat and while loops.

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

2.1 Understanding loops


A loop is a construct used in programming to repeatedly execute a block of statements multiple times. Let's look at the
structures that make up nested loops: for loop, repeat loop, and while loop.

2.1.1 For loop


A for loop executes one or more statements a specified number of times. When a loop is set to repeat a fixed number of times,
it is called an unconditional loop.

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.

for <LoopVariable> := <StartValue> to <EndValue> 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,

The following flowchart shows the logic of the for loop:

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]

Figure 1: Flowchart of the for loop.

2.1.2 Repeat loop


A repeat loop is a conditional loop that allows the programmer to execute the program statements continuously until a
specific condition is met. It is worthwhile to consider the following:

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) >

until < terminating condition(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]

iTotal := 1; : Initialise Initialise


iTotal := iTotal + iNumber; Change

until iTotal > 100; Test

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);

iTotal := iTotal + iNumber;

until iTotal > 100;

lblDisplay.Caption := IntToStr(iNumber)

The following flowchart shows the logic of the repeat loop:

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]

Figure 2: Flowchart of the repeat loop.

2.1.3 While loop


A while loop allows you to repeat one or more program statements based on the result of a condition. When writing a while
loop, first determine the continuation condition of the loop and the value that determines whether the loop should be
executed. Then build the loop according to the so-called ITC principle.

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]

while <loop-continuation condition> do

begin

<instruction(s)>

end;

The loop structure should adhere to the ITC principle.

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.

iNumber := 16; Initialise the variable


while iNumber < 100 do Test the variable

iNumber := iNumber + 16; Change the content of the variable

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 ;

The following flowchart represents the code above:

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]

Figure 3: Flowchart of the while loop.

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]

3. Working with loops


A string consists of text, but it can have numbers in it. The length function in Delphi determines the number of characters.

3.1 Working with a single character in a string


Let's say our string is called: 'sString', and we will assign a value ‘Welcome Piet’.
So, what Delphi will do is it will break the string ‘Welcome Piet’ into different components. Each component has its character
and its index value.

This string takes up 12 spaces of text. If you want to refer to one of the characters, you can use square brackets:

Figure 4: sString contents.

sString [3] returns the character l.

sString [9] returns the character P.

3.1.1 How do we join strings?


These are the structures to be used to join strings:

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

This is the Delphi code to be used;

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]

procedure TfrmCountLetter.btnCountClick(Sender: TObject);


var
iIndex, iNumLetters : integer;
sSentence : string;
cLetter : char;

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;

3.1.3 Example 2: Building a new string


Sometimes you will need to build a new string as you work through an existing string. The following example shows a code
that will shorten a sentence by removing the vowels.

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 or False: Loops in programming start counting from one.

 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy