Tutorial 3 - Repetition Control Structure
Tutorial 3 - 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
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 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
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
Do
<list of statements>
Loop {While | Until} <condition>
strPassWord = "BEAVIS"
intNumTries = 0
Do
strUserTry = InputBox("Enter password: ")
intNumTries = intNumTries + 1
Loop Until strUserTry = strPassWord Or intNumTries = 3
Repetition Control Structure
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.
' 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
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.
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:
Reference
Internet :
– http://www.thevbprogrammer.com/Ch05/05-04-RepetitionStructure.htm