Conditional Loops MR Long Summary
Conditional Loops MR Long Summary
Subject:
10
Information Technology
Version:
Topic:
Beta
Conditional Loops
Video Education
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.
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 <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
<statement> ;
….
<statement> ;
until <condition(s)> ;
NOTE: No begin end is needed.
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.
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.
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
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 ;
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
youtube.com/@MrLongITandCAT
facebook.com/MrLongEducation @MrLongEdu
tiktok.com/@mrlongeducation