You are on page 1of 10

Repetition Control Structure

Programming Principle

Repetition
Control
Structure
Tutorial 3
Repetition Control Structure

Group 12
Muhammad Zulhafiz Bin Abu Kasim
01DIP09F1002
HONG HUEY LIAN
01DIP09F1008
NOOR AKMAR LIA BINTI KAMARUZAMAN
01DIP09F1033

Lecturer’s Name : MDM. NORLIZA BT MOHAMED PIAH

The Repetition Control Structure


The repetition control structure is also known as the looping or iteration control
structure. Looping is the process of repeatedly executing one or more steps of an
algorithm or program; it is essential in programming, as most programs perform
repetitious tasks.
Every loop consists of the following three parts:
(1) The loop termination decision - determines when (under what condition) the
loop will be terminated (stopped). It is essential that some provision in the
program be made for the loop to stop; otherwise, the computer would
continue to execute the loop indefinitely - a loop that doesn't stop is called an
endless loop or infinite loop; when a program gets stuck in an endless loop,
programmers say that the program is "looping".
(2) The body of the loop - the step or steps of the algorithm that are repeated
within the loop. This may be only one action, or it may include almost all of
the program statements. Important note: some action must occur within the
body of the loop that will affect the loop termination decision at some point.
(3) Transfer back to the beginning of the loop - returns control to the top of the
loop so that a new repetition (iteration) can begin.
There are two basic types of loops: the pre-test loop and the post-test loop.
Repetition Control Structure

The Pre-Test Loop

The pre-test loop can be implemented in VB with a "Do" statement followed by


either the keyword "While" or "Until". "Do While" means execute the statements in
the body of the loop While a certain condition is true. "Do Until" means execute the
statements in the body of the loop Until a certain condition is true. While and Until
are opposites of each other. In other words, doing something Until a condition is
TRUE is the same as doing something While a condition is FALSE. For example, Do
While X <= 10 is the same as Do Until X > 10.

Both the "While" and the "Until" versions of the pre-test loop are flowcharted
below. The only difference between the two is the placement of the "True" and
"False" paths extending from the decision diamond.
Repetition Control Structure

With a pre-test loop, since the "loop termination decision" is tested at the top of
the loop, it is possible that the statements in the body of the loop will never be
executed.

The Do While (Do Until) Loop


The general format for a pre-test loop in VB is:

Do {While | Until} <condition>


<list of statements>
Loop

The Do While form of this statement works as follows: First, the condition is
evaluated. If the condition is TRUE, the list of statements is executed and the Loop
statement sends control back to the Do While statement, where the condition is re-
evaluated. If the condition is FALSE, program control branches to the statement
following the Loop statement.

The Do Until form of this statement works as follows: First, the condition is
evaluated. If the condition is FALSE, the list of statements is executed and the Loop
statement sends control back to the Do Until statement, where the condition is re-
evaluated. If the condition is TRUE, program control branches to the statement
following the Loop statement.

A typical application for the pre-test loop is the "input loop". The input loop works
as follows:
Repetition Control Structure

(1) A "priming", or initial Input is performed outside the loop. It obtains the initial
value for the variable that will control the loop termination decision (also called the
"loop control variable"; it will determine whether or not to enter the loop for the first
time).

(2) The loop begins with a Do While or Do Until statement. The condition following
one of these compares the loop control variable to a value that will signify the end
of the loop. Such values are called sentinel or terminating values.

(3) The body of the loop contains the statements that will process each set of input
values. The last statement in the body of the loop should be an Input to obtain
another value for the loop control variable.
Repetition Control Structure

The Post-Test Loop

With a post-test loop, the "loop termination decision" is tested at the bottom of the
loop, therefore the statements in the body of the loop will always be executed at
least one time.

In VB, post-test loops are implemented with the Do/Loop statements, however, the
"While" and the "Until" conditions appear after the keyword Loop, not Do. The
"While" and the "Until" versions of the post-test loop are flowcharted below. The
only difference between the two is the placement of the "True" and "False" paths
extending from the decision diamond.
Repetition Control Structure

The Do/Loop While (Loop Until) Loop

The general format for a post-test loop in VB is:

Do
<list of statements>
Loop {While | Until} <condition>

As an example of a post-test loop, the following is a code segment containing a


Do/Loop construct that allows a user three tries to get his password right:

Dim strPassWord As String


Dim strUserTry As String
Dim intNumTries As Integer

strPassWord = "BEAVIS"
intNumTries = 0

Do
strUserTry = InputBox("Enter password: ")
intNumTries = intNumTries + 1
Loop Until strUserTry = strPassWord Or intNumTries = 3
Repetition Control Structure

Exiting a Loop Early

Occasionally, it may be necessary or more efficient to exit a loop early (i.e., before
the loop termination decision is reached) due to some other condition that becomes
true as the loop is processing. For this situation the Exit Do or Exit For statement
can be used.

Example: Searching for a test value in an array – look at each element of the array 1
by 1, exiting when a match is found.

' variable declarations (whether some of these are declared at the


module level
' or in a local sub would depend on the application) ...

Dim astrNames(0 To 9) As String


Dim strTestName As String
Dim blnFoundIt As Boolean
Dim intX As Integer

' At some point in the program, after the array has been loaded and we
have
' a test name to look for, the search logic might look like this:

blnFoundIt = False

For intX = 0 To 9
If strTestName = astrNames(intX) Then
blnFoundIt = True
Exit For
End If
Next

If blnFoundIt Then . . .
Repetition Control Structure

Counting, Totaling and Averaging

Within loops, a variable that serves as a counter is frequently used to count how
many times the loop has been repeated, thus reflecting how many items have been
processed. You saw that the For/Next loop has a built-in counter; but a limitation of
the For/Next loop is that you must know in advance how many items will be
processed in the loop. With a Do While or Do Until loop, you typically do not know
in advance how many items will be processed. With Do While or Do Until, your
counter variable is typically initialized to zero or one, and you increment your
counter variable with an assignment statement that has the form:

Counter = Counter + 1

When the loop exits, the counter variable contains a number that reflects the
number of times the loop was executed.

Another type of variable frequently used within loops is a total or accumulator


variable. An accumulator variable is typically initialized to zero and then has an
amount added to it each time that the loop repeats. The amount that is added to
the accumulator variable will generally be from an item that was obtained directly
from input, or from a variable derived from a calculation based on one or more
input items. You add to your accumulator variable with an assignment statement
that has the form:

Total = Total + Value

When the loop exits, the accumulator variable contains a value that reflects the
accumulated total derived from repeatedly adding into it on each pass through the
loop.

If your application calls for an average, your loop must perform both counting and
totaling. When the loop exits, you can calculate the average by dividing the total by
the count, for example:

Average = Total / Counter


Repetition Control Structure

Reference

Internet :

– http://www.thevbprogrammer.com/Ch05/05-04-RepetitionStructure.htm

You might also like