MIC 110

Loop Structures

Objectives:

Do loops

The Do loop repeats a sequence of statements either as long as or until a certain condition is true.  A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statement.  The condition, along with either the word While or Until, follows the word Do or the word Loop.  Is it confusing enough?  Please, continue on.  It's a lot easier to understand them with examples.

Do While loop

When Visual Basic executes a Do-While loop as below,

            Do While condition

                        statement(s)

            Loop

it first checks the truth value of condition.  If the condition is false, then the statements inside the loop are not executed, and the program continues with the line after the Loop statement.  If the condition is true, then the statements inside the loop are executed.  When it reaches the Loop statement, the entire process is repeated, beginning with the testing of condition at the head of the Do-While statement.  In other words, the statements inside the loop are repeatedly executed only as long as (that is, while) the condition is true.  The figure below shows the pseudocode and flowchart for this loop.

Do While condition is true

Processing step(s)

Loop

Example: The following program displays numbers from 1 through 10 as the Display commend button is clicked. Study the change of values in intNum as the loop is repeated.

Private Sub cmdDisplay_Click( )
Loop 1
Loop 2
Loop 3
Loop 4
Dim intNum As Integer        

‘Display numbers from 1 to 10

The Value of intNum after each instruction is executed
intNum = 1
1

' Initialize intNum

       
Do While intNum <= 10
1
2
3
4
picDisplayNumbers.Print intNum;
1
2
3
4
intNum = intNum + 1
2
1
3
2
4
3
5
4
Loop
End Sub

In this example, incrementing the value of intNum is crucial (intNum = intNum + 1 ). Since intNum has the value used in testing the loop condition, the loop will repeat infinitely unless its value is incremented.

The GUI:

Here are the Form and Project files of the example.




Do Until loops

Do-While loop checks the condition at the top of the loop – that is, before the statements are executed.  Alternatively, the condition can be tested at the bottom of the loop when the statement Loop is reached.  When VB encounters a Do loop of the form

Do

            statement(s)

Loop Until condition

it executes the statements inside the loop and then checks the truth value of condition.   If condition is true, then the program continues with the line after the Loop statement.  If condition is false, then the entire process is repeated beginning with the Do statement.  In other words, the statements inside the loop are executed at least once and then are repeatedly executed until the condition is true.  The following figure shows the pseudocode and flowchart for this type of Do loop.

Do

            Statements(s)

Loop Until condition is true


Example:

Private Sub cmdDisplay_Click( )

    Dim intNum As Integer

    'Display the numbers from 1 to 10

    intNum = 1

    Do

        picNumbers.Print intNum;

        intNum = intNum + 1

    Loop Until intNum > 10

End Sub

In this example, incrementing the value of intNum is crucial (intNum = intNum + 1 ). Since intNum has the value used in testing the loop condition, the loop will repeat infinitely unless its value is incremented.




 

For...Next Loops

When you know exactly how many times a loops should be executed, a special type of loop, called a For ... Next loop, can be sued. 

The following is the basic structure of For … Next loop

For i = m To n

            statement(s)

Next i

            (where i is the control variable, m is the initial value, and n is the termination value.)

The pair of statements For and Next causes the statements between them to be repeated a specified number of times.  The For statement designates a numeric variable, called the control variable, that is initialized and then automatically changes after each execution of the loop.  Also, the For statement gives the range of values this variable will assume.  The Next statement increments the control variable.  If m <= n, then i is assigned the values m, m + 1, . . ., n in order, and the body is executed once for each of these values.  If m > n, then the For...Next loop is terminated and the execution resumes with the statement after the For...Next loop.

The following figure shows the flowchart of the pseudocode of a For … Next loop.

For i = m to n

            Processing step(s)

Next i

 

Example:

Private Sub cmdDisplay_Click( )

    Dim intNum As Integer

    'Display the numbers from 1 to 10

    For intNum = 1 To 10

        picNumbers.Print intNum;

        'notice that you don't need a statement to increment num

    Next intNum

End Sub

When program execution reaches For...Next loop, such as the one shown above, the For statement assigns to the control variable i (intNum) the initial value m and checks to see whether i is greater than the terminating value n.  If so, then execution jumps to the line following the Next statement.  If i <= n, the statements inside the loop are executed.  Then, the Next statement increases the value of i (intNum) by 1 and checks this new value to see if it exceeds n.  If not, the entire process is repeated until the value of i exceeds n.  When this happens, the program moves to the line following the loop.

 

Nested Loop

You can add another loop within a loop. The inner loop is called nested loop. The effect is adding another dimention to the process. For example, with one loop, you can do one task repeatedly. With a nested loop, you can do two tasks together.

Here is a sample codes of nested loop using the Do-While loop. However, you may use any combination of loops.

' Displays the numbers from 1 to 10 ten times
Option Explicit Private Sub cmdDisplay_Click()
Dim intColumnNum As Integer
Dim intRowNum As Integer

‘initialize the counters
intColumnNum = 1
intRowNum = 1

Do While intColumnNum <= 10
      Do While intRowNum <= 10
            picDisplayNumbers.Print intRowNum;
            intRowNum = intRowNum + 1
      Loop 'end of the inner loop


      picDisplayNumbers.Print ""       'truncates each line.
      intRowNum = 1       'resets the intRowNum for the next
      loop. intColumnNum = intColumnNum + 1
Loop       'end of the outter loop End
Sub

The following is the outcome of program.

Here are files for the Form and Project files of the sample program.

 

Exercises

Part 1

Based on the example files ( Form | Project ), convert the program to one with a Do-Until loop and another one with a For-Next loop.

Part 2

Write a program that reads temperatures and prints the average of them. Take a look at the sample program.

A possible answer key ( Form | Project )

Part 3

Download and open this Form (loop1.frm) and Project (loop1.vbp) files. Modify the program using a For loop.

Then add a new form to the project. The new form should use a For loop for getting the input numbers.

Add another form to the project. The new form should use a For loop for getting the input numbers. And ask how many numbers the user are going to enter, and use that number in the For loop.

Add another form to te project. At this time, use a Do-While loop to do the same.

Finally, add another form to the project. Use Do-Until loop to do the same.

The answer keys are available below. Download all files first, then open the Project file.

** A reference for Load/Unload method is in the Methods page.

Part 4

Based on the answer key files from Part 2 ( Form | Project ), add modified forms with different types of loops such For, Do-While and Do-Until.

Part 5

Based on the following files (Form and Project), create a program that prints numbers as below.

Hints: To terminate the inner loop, compare the counters from inner and outter loops and use a boolean variable.

The answer files (Form | Project)