Lesson V. Loop
Structures
Objectives
ü To be
able to use the Do-While-Loop, Do-Until-Loop, and For-Next repetition
structures
ü To
understand and implement counter-controlled repetition and sentinel-controlled
repetition
ü To understand
the concept of nested control structures
![]()
Loop Structures are statements
that execute instructions repeatedly. A
common practical application is when you need to compute 3^6. This expression is evaluated by multiplying
itself 6 times.
VB provides several Loop Structures. They are classified as Sentinel-controlled Structures and Counter-controlled Structures. Sentinel-controlled Loop Structures iterate routines until a special value called sentinel value contains a certain value to indicate. For example, we iterate until variable done has the value True. Variable done in this case is our sentinel. Counter-controlled repetition requires a counter variable (or sometimes called a loop counter). The counter variable is incremented (or decremented) every iteration. Loop terminates when the counter value reaches a particular value.
Syntax:
Do While
(<comparison_test>)
One or more VB Statements

The statements enclosed by Do-While
and Loop, called the loop body, are executed repeatedly while <comparison_test>,
a boolean expression, is True.
<comparison_test> is evaluated the first time the loop begins. Thus, if <comparison_test> is
initially False, the loop body will never execute. One of the statements in the loop body
should somehow set the <comparison test> to False to
terminate the loop. If it remains True,
VB will perpetually execute the statements.
This is called infinite loop and often causes the computer to
appear to have hung.
Syntax:
Do Until
(<comparison_test>)
One or more VB Statements

Do-Until loop
works exactly like the Do-While loop except that the Do-Until
loop continues executing the loop body until the comparison is True. Just like Do-While, Do-Until
evaluates <comparison_test> first to determine if the loop body
should be executed.
Syntax:
Do
One or more VB Statements

This
structure works exactly like the preceding two loop structures. Unlike the first two, the loop body is
executed first before evaluating <comparison_test>. If <comparison_test> is true,
VB repeats the loop body. Otherwise, VB
exits the loop and executes the statement following the loop code. Do-Loop-Until executes the loop body
at least once.
Syntax:
For <counter_var>
= <start_val> To <end_val> Step <increment_val>
One or more VB statements
Next <counter_var>
The For-Loop
also iterates a block of statements.
Unlike the other loop structures that we have discussed, For-Loop
iterates for a specified number of times.
The number of iterations is determined by <start_val> and <end_val>,
both Integers. Initially, <counter_var>,
an Integer variable, receives the value of <start_val>. <counter_var> is incremented by
the value of <increment_val> every iteration. By default, <increment_val> is
equal to 1. In this case, the loop
terminates when <counter_var> is greater than <end_val>. Thus, if <start_val> is equal
to 1 and <end_val> is equal to 5, then there would be 5
iterations. <counter_var>
will have the value 1 in the first iteration, 2 in the second iteration, and 5
in the last iteration. The Step
clause is optional (Step 1 by default).
If you assign a negative value to <increment_val>, Visual
Basic counts down. The Next
clause tells the For-Loop to add
<increment_val> to <counter_var> and checks if
it has not gone beyond <end_val>. The <counter_var>s
after For and Next should be the same variable.
Take the
following examples:
For x = 2 To
7
Loop Body
Next x
The loop
body is iterated 6 times. Value of x
is incremented by one every iteration (x=2 in the first iteration, x=3
in the second iteration, .. , x=7 in the last iteration).
For x = 2 To
7 Step 2
Loop Body
Next x
The loop
body is iterated 3 times. Value of x
is incremented by two every iteration (x=2 in the first iteration, x=4
in the second iteration, and x=6 in the last iteration). At the end of the third iteration, Next
x assigns the value 8 to x.
Since it is greater than 7, the <end_val>, the loop
terminates.
For x = 9 To
1 Step -3
Loop Body
Next x
The loop
body is iterated 3 times. Value of x
is decremented by three every iteration (x=9 in the first iteration, x=6
in the second iteration, and x=3 in the last iteration). At the end of the third iteration, Next x
assigns the value 0 to x. Since
it is less than 1, the <end_val>, the loop terminates.
The
following Do-While and For-Loop structures are the same:
|
For x = 1 To
8 Step 2 Loop Body Next x |
x= 1 Do While
(x<=9) Loop Body x = x + 2 Loop
|
For-Loop is a
perfect structure for programs that requires a specific number of iterations (instead
of a condition which is met at no specific time) or for a program that needs to
access a sequence of values.
Let’s
create an application that asks for an integer and outputs the summation of
that number. E.g. the summation of 5 is
15 (1+2+3+4+5).

Private
Sub cmdSum_Click()
If (Val(txtNum.Text) > 0) Then
intSum = 0
For x = 1 To
Val(txtNum.Text)
intSum = intSum + x
Next x
lblResult.Caption = "Summation of
" & txtNum.Text & " is " & Str(intSum) &
"."
Else
lblResult.Caption = "Enter a
positive integer only."
End If
The If
statement makes sure that before we compute for the summation, the number
entered should be a positive number (otherwise, our program will bear an
incorrect result). intSum = intSum
+ x is done Val(txtNum.Text) times. If the user entered 3, then we will be
executing this statement thrice. On the
first iteration, intSum is assigned the value 1. On the second iteration, the value of x
(which is 2 at the moment) is added to intSum, eventually assigning the
value 3 to intSum. Finally, the
next value of x (which is 3) is added to intSum giving it the
value 6. Since Next x
increments x to 4, and 4 is greater than 3 (our <end_val>),
we terminate the loop and display the value of intSum, the summation of
3.
intX = 20
Do While (intX
>= 1)
intX = intX + 1
Loop

[ Home ]