You are on page 1of 36

Unit 2

CONTROL STATEMENTS
AND FUNCTIONS

Lesson 2.3

LOOPING

Objectives:
At the end of the lesson, you should be able to:
1. Create Loops using For-Next Statements and
2. Control the number of iterations in a loop
using Step.

Loops
At instances when we want the computer to do
a task repeatedly for a certain number of times,
looping becomes necessary. Note that this is not
the same as having a user re-enter a value until a
valid value is given (like our SK program in
Lesson 2.2). In such a case it is the user doing the
repeated task, not the computer. The loop was
first conceptualized by Ada Byron (recall Lesson
1.1).

The For-Next Loop


One VB statement that allows looping is called
the For-Next statement or loop.
For variable = start value To end value Step increment
Statements
Next

The For-Next Loop

Continuation

This loop repeats a set of statements a certain


number of times. That number is determined by
three values-the start value, the end value, and
the increment. Note that the statements to be
repeated are enclosed by For and Next. Next is
the end marker of the loop just as End Sub is the
end marker for a subroutine. The variable used in
a For-Next statement may be used in the
statements between For and Next.

The For-Next Loop

Continuation

In order to understand this statement better,


we shall conduct a little experiment.

Try this on your computer

Try this on your computer

Continuation

Name the text boxes (from top to bottom) as


txtStart, txtEnd and txtIncr. Then, name, too,
the command button as cmdOk and the list box
on the right as lstLoop. Declare x, start_val,
end_val, incr as variables of type Single. Finally,
assign the following codes to cmdOk.

Try this on your computer

Continuation

Private Sub cmdOk_Click ( )


lstLoop.Clear
start_val = Val (txtStart.Text)
end_val = Val (txtEnd.Text)
incr = Val (txtIncr.Text)
For x = start_val to end_val Step incr
lstLoop.AddItem (*)
Next
End Sub

Try this on your computer

Continuation

Save the program; then, run it. Take note of the


number of * that appears as you change the
values in the text boxes.

The For-Next Loop

Continuation

Note that the object which looks like a huge


text box is actually a list box.
A list box is different from a text box, for you
can add items to it via the statement
ListBox_name. AddItem (items to be added). In
our program, we shall add * to the list box. This
will be repeated several times depending on the
values we enter in the text boxes. The statement,
1stLoop.Clear, is used to clear the contents of the
list box prior to loading it with *s.

The For-Next Loop

Continuation

In the course of running the program, you may


have come across the following outputs.
For
Numb
Statement er of
*s
For x = 1
10
To 10
Step 1
For x = 1
3
To 6 Step
2
For x = 3
1
To 5 Step

The For-Next Loop

Continuation

If youve tried entering a zero increment, you


may have noticed something unusual VB
hanged!
Warning!
A zero increment will result in an infinite loop a
loop that never terminates.

Why this happens will become evident once


we understand how the values affect the number
of iterations or loops.

Continuation

The For-Next Loop

Below is a flowchart that illustrates what takes


place in our For-Next loop.
x = start_val
Yes

x > end_val
add an * to 1stLoop
x = x + incr
end

No

The For-Next Loop

Continuation

What happens at first is that our dummy


variable x takes on the value of start_val. It (x) is
then checked if it is greater than end_val. While
it (x) is not greater than end_val, two things
happen: An * is added to the list box and incr is
added to the value of x via the statement x = x +
incr.

The For-Next Loop

Continuation

For example, consider For x = 1 To 5 Step 3.


Initially x is assigned the value of 1. Since it is not
yet greater than 5, an iteration is made and 3 is
added to it making it 4. Since it is not yet greater
than 5, another iteration is made and 3 is added
to it, making it 7. Now, that it is greater than 5,
the iterations cease. A total of two iterations were
made; thus, we see two *s

The For-Next Loop

Continuation

When the increment is zero, x will never reach


or surpass the end value; hence, we have an
infinite loop.

Using the Dummy Variable


Now that we understand how For-Next works,
there are other things we should learn about it.
One is that For x = 1 To 10 Step 1 is equivalent to
For x = 1 To 10. What it means is that if the
increment is 1, we do not have to add Step 1. The
other is that the dummy variable can be used.

Try this on your computer


In the Loop Tester program, change the
statement 1stLoop.AddItem (*) to
1stLoop.AddItem (x) and run the program,
taking note of the outputs as you change the
values in the text boxes.
We can now see the values x holds in a loop.
Keep this in mind as we tackle another program.

The Prime Evaluator


When is a number N prime? N is prime if N is
greater than 1 and the only divisors of N are 1 and
N (itself). For example, 5 is prime since it is
divisible by 1 and 5 only. In order to check if N is
prime, we need to test that none of the numbers
from 2 to N-1 is a divisor of N. Otherwise, N is
not prime.

The Prime Evaluator

Continuation

For example, to test if 6 is prime, we need to test


the numbers 2 to 5 against it. Of course, we shall
find that 6 is perfectly divisible by 2 and 3; hence,
6 is not prime. The question is this: How do we
check for divisibility? The answer lies in two
mathematical operators division (/) and
integer division (\).

The Prime Evaluator

Continuation

Note that 6 is divisible by 3 because 6/3 is equal


to 6\3 (since 6/3 and 6\3 are both equal to 2).
On the other hand, 6 is not divisible by 4 since
6/4 is not equal to 6\4 (since 6/4 = 1.5 and 6\4 =
1). Thus, we can say that N is divisible by X if and
only if, N/X = N\X. Now, all we need is for X to
vary from 2 to N-1.

The Prime Evaluator

Continuation

Let us assume that we have a form with a


command button cmdOk, a text box txtN, and a
label lblResult. What should happen is that the
user supplies a number via txtN, clicks cmdOk,
and views the result of whether the number was
prime or not in lblResult.

The Prime Evaluator

Continuation

We also need two variables: N to held the


number supplied by the user and X to hold the
numbers to be tested against N for divisibility.
In code,
Dim N, X As Integer
Private Sub cmdOk_Click( )
N = Val (txtN.Text)
End Sub

The Prime Evaluator

Continuation

Now, we need to test if X perfectly divides N,


because if it does, N is not prime.
In code,
Dim N, X As Integer
Private Sub cmdOk_Click( )
N = Val (txtN.Text)
If N/X = N\X Then
lblResult.Caption = Not Prime
End If
End Sub

The Prime Evaluator

Continuation

Notice that for this to work, X must vary in value


from 2 to N-1. This is where the For-Next loop comes
in.
In code,
Dim N, X As Integer
Private Sub cmdOk_Click( )
N = Val (txtN.Text)
For X = 2 To N-1
If N/X = N\X Then
lblResult.Caption = Not Prime
End If
Next
End Sub

The Prime Evaluator

Continuation

In hindsight, it seems a waste of computing


energy to test all Xs when we have already
encountered an X which perfectly divides N.
For example, the computer has found that 2
perfectly divides 6, so it does not need to check if
3, 4, or 5 also perfectly divides 6. We need to find
a way to step out of the loop once this happens.
Fortunately, there is a statement that allows that.

The Prime Evaluator


Dim N, X As Integer
Private Sub cmdOk_Click( )
N = Val (txtN.Text)
For X = 2 To N-1
If N/X = N\X Then
lblResult.Caption = Not Prime
Exit For
End If
Next
End Sub

Continuation

Try this on your computer


Construct the Prime Evaluator program weve
just discussed. Verify if it works.

The Prime Evaluator

Continuation

You probably noticed that the program does


work when N is not prime. When N is prime,
however, it doesnt say so. I will leave it up to you
to experiment where in the code this line should
be placed.
lblResult.Caption = Prime!

Nesting Loops
In the previous lesson, we found that we can nest
If statements. We can do the same for For-Next.
The rules of nesting still apply, of course.

Nesting Loops

Continuation

Here is an example:
For A = 1 To 5
For B = 1 to 10
(Statements)
Next
Next
In this example, for every loop of A, B has made ten
loops. Since A will loop five times, the statements
bounded by the B loop would have been repeated
fifty times!

Other Loops
Aside from For-Next, there are other looping
statements that can be used. The three sets of
code outlined below are essentially the same.

For K = 5 to 10 Step 2
(Repeated Statements)
Next

Other Loops
K=5
While K < = 10
(Repeated Statements)
K=K+2
Wend

Continuation

Other Loops

Continuation

K=5
Do While K < = 10
(Repeated Statements)
K=K+2
Loop
The last two sets of code are more literal
translations of the flowchart that was shown
earlier.

You might also like