Lecture 7: Loop Structures
Outline
In this lecture, we will discuss loop structures…
Our second class of VB structures for controlling program flow.
We will discuss two broad classes of loops: 1. For…Next Loops
The For…Next Loop The For Each…Next Loop
2. Do…Loop Loops
The Do While…Loop Loop The Do Until…Loop Loop The Do…Loop While Loop The Do…Loop Until Loop
Combining Loops:
Nested Loops
Loop Structures
So far, our programs have been acyclic:
We have discussed both linear and branched programs…
With no repeated processes (cycles).
However, real algorithms often use of loops:
i.e., the programmed (controlled) repetition of a basic process.
Example: Make a telephone bill for all of your customers (not just one).
The ability to repetitively cycle a process is called iteration.
Blocks of code for looping are called Loop Structures.
As noted in the outline, VB .NET provides several flavors of loops:
For…Next loops Do…While loops
Each of these provides a means to control program flow…
In which process paths are executed once or not at all.
Via controlled cycling.
Let’s take a look at the simplest: The For…Next loop.
The For…Next Loop
The simplest and most popular loop structure is the For…Next loop.
This allows the repeated execution of a set of statements…
With loop termination based on a loop index.
The VB syntax for the For…Next loop is: For index=start To End Step step statements Next
index: is a loop variable which:
End specifies a loop condition , evaluated at each iteration:
Indexes (counts) the number of loop iterations; Is Initialized to the value, start Determines whether statements are executed. If index < start, conditions are executed; otherwise, the loop ends.
Step sets the increment value of the loop (default = 1):
After each execution of ‘statements’, index is incremented by step; z
In other words: index += step
Note: if step is negative, you can loop backwards.
Next index : tells the running program to loop back to ‘For index’…
And test the loop condition, based on index, z to see if another cycle is to be executed.
Flow Chart for the For…Next Loop
Example: ‘Calculate the sum of all integer values from 1 to 10.’
We use a For…Next loop
With loop variable, i to both index the 10 integers and control looping. And variable Total to store the sum.
VB code: Dim Total, i As Integer Total = 0 For i=1 To 10 Step 1 Total = Total + i Next …
The For…Next loop’s flow of control is shown clearly using a Flow Chart:
Our loop is executed 10 times.
Starting cycle 1: i = 1; Total = 0; After cycle 10: i= 11; Total = 55. The i=11 value fails the test, which stops the loop.
Program: Simple For…Next Loop
Let’s create a simple For…Next program…
lstData
Program: Simple For…Next Loop
Do…Loop Loops
A different type of loop structure is a Do…Loop loop:
Allow repeated execution of a set of statements…
Can be used to execute a loop an indefinite number of times.
With loop termination based on a conditional test (True/False). Compare with a For…Next loop (set number of cycles).
There are 4 flavors of Do…Loop loops:
Made using two keywords: While and Until… Loop Condition tested at the start of each loop cycle: z
Do While…Loop loop z
z
Do Until…Loop loop z
z
Loops while a condition is true. Loops until a condition becomes true.
Note: loop body statements might be executed 0 times.
Loop Condition tested at the end of each cycle: z
Do…Loop While loop z
z
Do…Loop Until loop z
z
Loops while a condition is true Loops until a condition becomes true
Note: loop body statements are executed at least 1 time.
The Do While…Loop Loop
The Do While…Loop loop:
Keeps looping while a loop condition is True; Tests the condition at the start of each cycle.
Halts when the condition becomes False.
The VB syntax for the Do While…Loop loop is: Do While condition statements Loop
Keyword Do says, ‘start the cycle’
Keyword While says ‘test our condition’:
Tested at the start of each loop… z If condition Æ True, statements are executed once;
Example: (sum < 1000)
Keyword Loop then tells control to loop (return to Do)…
And another cycle begins
The Do Until…Loop Loop
The Do Until…Loop loop:
Keeps looping while a loop condition is False; Tests the condition at the start of each cycle.
Halts after the cycle in which the condition becomes True.
The VB syntax for the Do Until…Loop loop is: Do Until condition statements Loop
Keyword Do says, ‘start the cycle’
Keyword Until says ‘test our condition’:
Tested at the start of each loop… z If condition Æ False, statements are executed once;
Example: (sum >= 1000)
Keyword Loop then tells control to loop (return to Do)….
And anther cycle begins.
The Do…Loop While Loop
The Do …Loop While loop:
Keeps looping while a loop condition is True; Tests the condition at the end of each cycle.
Halts after the cycle in which the condition becomes False.
The VB syntax for the Do…Loop While loop is: Do statements Loop While condition
Keyword Do says:
‘Execute our statements once’.
Keywords Loop While say ‘test our condition’:
Tested at the bottom of each loop… z If condition Æ True, control returns to Do… z And another cycle begins. z Otherwise, the loop ends.
Example: (sum < 1000)
The Do …Loop Until Loop
The Do …Loop Until loop:
Keeps looping while a loop condition is False; Tests the condition at the end of each cycle.
Halts after the cycle in which the condition becomes True.
The VB syntax for the Do …Loop Until loop is: Do statements Loop Until condition
Keyword Do says:
‘Execute our statements once’.
Keywords Loop Until say ‘test our condition’:
Tested at the bottom of each loop… z If condition Æ False, control returns to Do… z And another cycle begins. z Otherwise, the loop ends.
Example: (sum >= 1000)
Nested Loops
Sometimes it is desirable to place loops inside of loops.
These are referred to as Nested Loops… Multiple loops of all types may be nested in the obvious way…
Example: A simple nested For…Next loop…
Purpose: Display all pairs of positive integers, each <=10, in a ListBox called lstData. For i=1 To 10 For j=1 To 10 lstData.Items.Add(i & “, ” & j) Next Next
Loops: Quitting Early
You may quit a loop pre-maturely (early)…
For instance, you may complete the task early…
This is done using the statement:
E.g., and If…Then structure.
When quitting early, control is transferred:
Exit For (For…Next Loops) Exit Do (Do Loops)
Usually used in combination with a conditional statement…
And not need to continue looping.
To the statement after the last control statement of the loop. Thus, for nested loops, the current loop is exited.
Note: There are equivalent Exit statements for other structures:
Exit Select, Exit If, Exit Function, Exit Sub, etc.
Program to Demonstrate Loops
Program (cont.)
Program (cont.)
Program (cont.)
Program (cont.)