0% found this document useful (0 votes)
39 views17 pages

CSM 157 PROGRAMMING-Week5

Looping control structures repeat execution of statements. Common loops include FOR, WHILE, DO-WHILE, and REPEAT-UNTIL. A FOR loop repeats a specified number of times using a control variable. A WHILE loop repeats while a condition is true. DO-WHILE and REPEAT-UNTIL loops are similar but the condition is checked after/until execution respectively, guaranteeing the statements run at least once. Loops allow efficient repetition of tasks without rewriting identical code.
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)
39 views17 pages

CSM 157 PROGRAMMING-Week5

Looping control structures repeat execution of statements. Common loops include FOR, WHILE, DO-WHILE, and REPEAT-UNTIL. A FOR loop repeats a specified number of times using a control variable. A WHILE loop repeats while a condition is true. DO-WHILE and REPEAT-UNTIL loops are similar but the condition is checked after/until execution respectively, guaranteeing the statements run at least once. Loops allow efficient repetition of tasks without rewriting identical code.
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/ 17

Looping control structure

• Also known as the repetitive or iterative control structure


• Use to repeat the execution of statement(s) for specified number of
times or until when a certain condition is met.
• The common looping statements are as follows:
1. FOR loop
2. WHILE loop
3. DO-WHILE loop
4. REPEAT-UNTIL loop
FOR loop Statement
• Best used when the number of times to loop is known in advance

• Usually has three commands/statements

- The first is used to set a starting value

- The second is the end condition or the last value

- The third is used to modify a value used in the block. The modifier cannot be 0.

• Also known as a pre-test loop. Thus, the loop condition is tested before the loop
statement(s) is/are executed.

• The general structure is as follows:


FOR loop Statement
FOR control_variable = StartingValue TO EndingValue [STEP value]
loop statement(s)
ENDFOR
Where
StartingValue is used to intialise the control variable. Control_variable is a
variable used to regulate the looping or the repetition of the loop.
EndingValue specifies the end condition or the last value of the control
variable
STEP value is optional. When used, it specifies how the value of the
control variable should be modified for the next iteration. For all values
other than 1, the STEP value must be specified. If the STEP value is not
specified then it assumes the default value of 1
FOR loop Statement
Example 1: Assume we want a loop to display the numbers 1 to 100. Let i be
our control variable. The loop can be written as follows:
FOR i=1 TO 100 STEP 1
PRINT i
ENDFOR
In the above example,
i. The control variable (i) is first initiliased to 1
ii. The value of the control variable is then checked against the 100 to see if
we are within the range and will it be possible to get to the 100
iii. If (ii) is true, the statement PRINT i displays the current value of i.
iv. ENDFOR causes the loop to repeat by first checking (ii)
v. The looping stops when (ii) becomes false. For example when i is 101 the
control variable will be out of range and hence causes the looping to
stop
FOR loop Statement
Example 2: Assume we want a loop to print all even numbers between 1
and 500.
Solution: Let our control variable be EvenNumbers. Note that the first even
number in the range is 2 and successive even numbers differ from each
other by 2. We can write our loop as follows:

FOR EvenNumbers = 2 TO 500 STEP 2


PRINT EvenNumbers
ENDFOR

Note that the STEP value is 2 because of how successive even numbers
differ from each other.
FOR loop Statement
Example 3: Indicate how many times the statement “I Love
Programming” will be displayed on the screen.

FOR ManyTimes = 1 to 100 STEP -1


PRINT “I Love Programming”
ENDFOR

Answer: 0
Reason: If we start from 1 and our step value is -1 there is no way one
can ever get to 100 hence the loop is invalid and will not be executed at
all.
FOR loop Statement
For each of the following loops, indicate the number of times the loop will run.
• FOR I=100 to 40
:
ENDFOR
• FOR I= 100 to 40 STEP 3
:
ENDFOR
• FOR I=100 to 400 STEP -1
:
ENDFOR
• FOR I=100 to 400 STEP 1
:
ENDFOR
WHILE loop
• Best used when the number of times to loop is not known in advance
• The loop repeats as long as a condition has been met
• Is also known as a pre-test loop, thus, the condition is tested before
each execution of the loop
• The general structure is as follows:
WHILE (condition is true)
statement(s)
ENDWHILE

Where
condition is true is a relational expression
WHILE loop
Example 1
i=1
WHILE (i <= 100)
PRINT i
I = i+1
ENDWHILE
(i) In the above example, i controls the loop hence i is the control variable.
(ii) The control variable is first initialized to 1 before the start of the loop
(iii) Starting the loop, the value of i is checked to see if it is less than or equal
to 100
(iv) Since (iii) is true, the loop statements are executed by displaying the
value of i and then incrementing the value of i by 1
(v) The loop is repeated until the value of i is 101 which will make i<=100
false
WHILE loop
Example 2: Example of when it is best to use a WHILE loop. Assume we
have a certain number of numbers to sum and that we are told the
number 100 is not part of the list and that it just mark the end of the
list. Here, we don’t know the total number of the numbers in the set.
Our loop can be written as follows:
DECLARE sum, number AS INTEGER
sum=0
INPUT number
WHILE (number != 100)
sum = sum + number
INPUT number
ENDWHILE
WHILE loop

First line: Allows for the first number in the set to be entered

Second line: Checks to see if the number entered is not 100

Third line: Adds the number entered if it is not a 100

Fourth line: request for the next number in the set

Fifth line: Causes the loop to be executed again, if needed


DO-WHILE statement
• Best used when the number of times to loop is not known in advance
• The loop repeats as long as a condition has been met
• Is also known as a post-test loop, thus, the condition is tested at the end of each
execution of the loop
• This loop is guaranteed to execute at least one (1) time
• The general structure is as follows:
DO
statement(s)
WHILE (condition is true)

Where
condition is true is a relational expression
DO-WHILE statement
Example 1
i=1
DO
PRINT i
i = i+1
WHILE(i <= 100)

(i) In the above example, i controls the loop hence i is the control variable.
(ii) The control variable is first initialize to 1 before the start of the loop
(ii) The loop statements are then executed by displaying the value of i and then
incrementing the value of i by 1
(iv) The value of i is checked to see if it is less than or equal to 100
(v) The loop is repeated until the value of i is 101 which will make i <=100 false
DO-WHILE statement
Using the same example 2 under the WHILE loop,
the solution can be written as follows using the DO-
WHILE
INPUT number
DO
sum = sum + number
INPUT number
WHILE (number != 100)
REPEAT-UNTIL
The REPEAT UNTIL is functions exactly the same way as the DO-WHILE.
Most programming language have either the DO-WHILE or the REPEAT-
UNTIL and not both

In a DO-WHILE, simply replace the DO and the WHILE with REPEAT and
UNTIL respectively
Example 1
i=1
REPEAT
PRINT i
i = i+1
UNTIL (i <= 100)
REPEAT-UNTIL statement
Example 2
INPUT number
REPEAT
sum = sum + number
INPUT number
UNTIL (number != 100)
LOOPING
• Please note that for software development purposes you are to use
any of the looping statement you are comfortable with. However, for
examination purposes you can be restricted to which loop to use in
solving a given problem

• Having dealt with the control structures, we are now in a position to


try our hands on some examples.

You might also like