0% found this document useful (0 votes)
6 views6 pages

Conditional Loops MR Long Summary

The document provides an overview of conditional loops in programming, specifically focusing on WHILE and REPEAT loop statements. It explains the structure, execution, and differences between these loops, including the ITC principle for WHILE loops and the ICT principle for REPEAT loops. Additionally, it covers loop control methods and includes resources for further learning.

Uploaded by

kiara.amy2403
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)
6 views6 pages

Conditional Loops MR Long Summary

The document provides an overview of conditional loops in programming, specifically focusing on WHILE and REPEAT loop statements. It explains the structure, execution, and differences between these loops, including the ITC principle for WHILE loops and the ICT principle for REPEAT loops. Additionally, it covers loop control methods and includes resources for further learning.

Uploaded by

kiara.amy2403
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/ 6

Mr Long Grade:

Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education

Mr L ong ON CONDITIONAL LOOPS

Iteration Programming
• THREE ways the code is executed:
o Sequential – when each line is executed in order from first line to the last line.
o Selection – when you select which code will be executed based on a condition.
o Iteration – repeatedly execute code based on a condition.

• Example of Iteration using a flowchart: Count  7

RECAP: For Loops


for K := 1 to 10 do Is iNum NO
• When you know how many times to
<= 100
execute the loop before it starts.
o Sum the first 100 numbers
o Amount in a savings account after
YES
2 years

Do Code

iNum  iNum + 5

Other Code

Conditional Loops
• When you DON’T know how many times to execute the loop before it starts.
• Reason to stop the loop occurs inside the loop
o Sum all the numbers until the sum is greater than 100.
o How long it takes to save money in a savings account until you double your
money.

1
Mr Long Grade:
Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education

WHILE Loop Statement


General Structure – WHILE Loop Statement

while <condition(s)> do
begin
<statement> ;
….
<statement> ;
end ;
NOTE: Although you don’t need the begin end if only executing one statement, I suggest always using
them so that you don’t get confused if you decide to add more statements to the WHILE loop statement
later.
WARNING: Do NOT place a semi-colon (;) after the do operator.

REPEAT Loop Statement


General Structure – REPEAT Loop Statement

repeat
<statement> ;
….
<statement> ;
until <condition(s)> ;
NOTE: No begin end is needed.

WHILE Loop REPEAT Loop


while <condition(s)> do
repeat
begin
<statement(s)>
<statement(s)>
until <condition(s)> ;
end ;
Pre-condition Loop Post condition Loop
Condition is checked at START of loop Condition is checked at END of loop
0-ꝏ 1-ꝏ
Loop can run from 0 times to infinity because Loop will run at least once to infinity because
condition is checked first. condition is only checked at the end.
Condition(s) Condition(s)
TRUE means continue looping TRUE means stop loop
FALSE means stop loop FALSE means continue looping
NO begin..end needed because statements are
Begin..end needed for multiple statements
between repeat and until operators.

2
Mr Long Grade:
Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education

Condition(s)
• Condition must either result in a TRUE or a FALSE
o Same as conditions used in IF statements
o Use comparison operators { equal (=), greater than (>), less than (<),
greater than or equal to (>=), less than or equal to (<=), not equal (<>) }
o Multiple conditions use AND or OR operators
o Can make use of NOT operator, Boolean functions and sets.

RECAP: For Loops • Adds ALL numbers from 1 to 100


iSum := 0 ; • For loops have a built-in looping
for K := 1 to 100 do variable (K in example)
begin o Looping variable always has a
iSum := iSum + K ; starting value.
end ; o Looping variable increases by 1

WHILE Loop Example REPEAT Loop Example


iSum := 0 ;
iSum := 0 ;
K := 1 ;
K := 1 ;
while iSum < 100 do
repeat
begin
iSum := iSum + K ;
iSum := iSum + K ;
inc ( K ) ;
inc ( K ) ;
until iSum >= 100 ;
end ;
Explanation: Add all the numbers from 1 onwards UNTIL the sum of those numbers is 100 or more.
Note: The changes of the Sum equalling 100 exactly is highly unlikely so that is why you use 100 or more.
NOTE: Need a looping variable but While and Repeat loops do not have a built-in looping
variable. Therefore, you need to program one into the code:
o Looping variable always has a starting value. ( K := 1 )
o Looping variable increases by 1 ( inc( K ) ; )
Condition: iSum < 100 Condition: iSum >= 100
While iSum isn’t 100 or more, keep loop. Repeat the loop until iSum is 100 or more.
When iSum is 100 or more, stop the loop. Note the condition is the OPPOSITE of the while loop.

3
Mr Long Grade:
Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education

Infinite Loop
• Infinite loop is when the condition to stop the loop is never reached and so the loop will
continue to loop and never stop.
• To stop an infinite loop in Delphi, click on Run tab and then Program Reset.

WHILE Loop Example REPEAT Loop Example


iSum := 0 ;
iSum := 0 ;
K := 1 ;
K := 1 ;
while iSum _=_ 100 do
repeat
begin
iSum := iSum _– K_ ;
iSum := iSum + K ;
inc ( K ) ;
inc ( K ) ;
until iSum >= 100 ;
end ;
Note: Condition is iSum = 100 NOTE: iSum – K ;
The sum of all incremental numbers will never iSum variable starts at 0 and is continuously
equal 100 exactly. The iSum variable will decreasing in the loop. It will never reach the value of
eventually exceed 100 it will continue to add new 100 or more as it is always decreasing. This results in
values onto iSum and never decrease, therefore an Infinite Loop.
never get to 100. This results in an Infinite Loop.

ITC or ICT Principle


• When using a While loop, you must decide on a value(s) or variable(s) that will
determine if the loop is executed and when it stops.
• Referred to as the ITC principle:
The variable or variables must be assigned default values before the
Initialise While loop condition is checked.
The variable or variables are tested in the loop. If the condition is TRUE
Test then the code in the loop is executed.
Somewhere in the loop, the variable or variables, that are tested,
Change should change.

Example 1
Explanation:
K := 1 ; Displays the first 100 numbers that
iCount := 0 ; are divisible by BOTH 7 and 3.
while iCount < 100 do
begin
if ( K MOD 7 = 0 ) AND ( K MOD 3 = 0) then ITC Principle:
begin Variable that determines when loop
memDisplay.lines.add( IntToStr( K ) ) ; must stop: iCount
_inc ( iCount ) ;_ Initialise: iCount := 0 ;
end ; //end of if Test: iCount < 100
inc ( K ) ; Change: _inc( iCount ) ; _
end ; //end of while

4
Mr Long Grade:
Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education

• When using a Repeat loop, because the test occurs at the end (post condition loop),
rather use the ICT principle:
The variable or variables must be assigned default values before the
Initialise Repeat loop condition is checked.
Somewhere in the loop, the variable or variables, that are tested,
Change should change.
The variable or variables are tested in the loop. If the condition is FALSE
Test then the code in the loop is executed.

Example 2
Explanation:
Total ALL numbers that are divisible
K := 1 ;
by BOTH 7 and 3 until the total is
iSum := 0 ;
300 or more.
repeat

if ( K MOD 7 = 0 ) AND ( K MOD 3 = 0) then ITC Principle:


begin Variable that determines when loop
_iSum := iSum + K ;_ must stop: iSum
end ; //end of if Initialise: iSum := 0 ;
inc ( K ) ; Change: _iSum := iSum + K ; _
Test: iSum >= 300
until iSum >= 300 ;

Loop Control
Loops are controlled in different ways: iSum := 0 ;
• Counter controlled loops: looping variable is for iCounter := 1 to 10 do
begin
initialised and its value is changed inside the loop iSum := iSum + iCounter ;
end ;

• Sentinel controlled loops: variable


repeat
controlling the loop is tested for a //do code
sentinel or signal value. sAns := Inputbox( ‘’ , ‘Execute code again [Y/N]:’ , ‘’ ) ;
until sAns = ‘N’ ;

iSum := 1 ;
• Result controlled loops: loop continues until a result
while iSum <= 100 do
has been met. begin
iSum := iSum * 2 ;
end ;

5
Mr Long Grade:
Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education

Additional Links:
• Youtube video playlist:
https://www.youtube.com/watch?v=NBurr5mJ1Ys&list=PLxAS51iVMjv-rGgtTMpzUzVEFMqj2Zcjb
• Google drive resource activities:
https://tinyurl.com/MLE-G10IT-ConditionalProgramming

For more IT related material find us on:

youtube.com/@MrLongITandCAT

facebook.com/MrLongEducation @MrLongEdu

tiktok.com/@mrlongeducation

You might also like