Lecture 7 - Loop Structures
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…
In which process paths are executed once or not at all.
With no repeated processes (cycles).
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…
With loop termination based on a conditional test (True/False).
Can be used to execute a loop an indefinite number of times.
Compare with a For…Next loop (set number of cycles).
z Do While…Loop loop
z Loops while a condition is true.
z Do Until…Loop loop
z Loops until a condition becomes true.
z Note: loop body statements might be executed 0 times.
Loop Condition tested at the end of each cycle:
z Do…Loop While loop
z Loops while a condition is true
z Do…Loop Until loop
z Loops until a condition becomes true
z 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.
For i=1 To 10
For j=1 To 10
lstData.Items.Add(i & “, ” & j)
Next
Next
Loops: Quitting Early