lecture 7 - loop structures
TRANSCRIPT
-
8/14/2019 Lecture 7 - Loop Structures
1/20
Lecture 7: Loop Structures
-
8/14/2019 Lecture 7 - Loop Structures
2/20
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. ForNext Loops The ForNext Loop
The For Each
Next Loop
2. DoLoop Loops The Do WhileLoop Loop
The Do Until
Loop Loop The DoLoop While Loop
The DoLoop Until Loop
Combining Loops: Nested Loops
-
8/14/2019 Lecture 7 - Loop Structures
3/20
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).
However, real algorithms often use of loops:
i.e., the programmed (controlled) repetition of a basic process.
Example: Make a telephone bill for allof 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:
ForNext loops DoWhile loops
Each of these provides a means to control program flow
Via controlled cycling.
Lets take a look at the simplest: The ForNext loop.
-
8/14/2019 Lecture 7 - Loop Structures
4/20
The ForNext 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 ForNext loop is:
For index=startTo EndStep stepstatements
Next
index: is a loop variable which: Indexes (counts) the number of loop iterations; Is Initialized to the value, start
End specifies a loop condition , evaluated at each iteration: Determines whether statementsare 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, indexis incremented by step; 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,
to see if another cycle is to be executed.
-
8/14/2019 Lecture 7 - Loop Structures
5/20
Flow Chart for the ForNext Loop
Example: Calculate the sum of all integer values from 1 to 10.
We use a ForNext 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 ForNext loops flow of control isshown 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.
-
8/14/2019 Lecture 7 - Loop Structures
6/20
-
8/14/2019 Lecture 7 - Loop Structures
7/20
Program: Simple ForNext Loop
-
8/14/2019 Lecture 7 - Loop Structures
8/20
DoLoop 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).
There are 4 flavors of DoLoop loops: Made using two keywords: While and Until
Loop Condition tested at the start of each loop cycle:
Do WhileLoop loop Loops while a condition is true.
Do UntilLoop loop
Loops until a condition becomes true.
Note: loop body statements might be executed 0 times. Loop Condition tested at the end of each cycle:
DoLoop While loop
Loops while a condition is true
DoLoop Until loop Loops until a condition becomes true
Note: loop body statements are executed at least 1 time.
-
8/14/2019 Lecture 7 - Loop Structures
9/20
The Do WhileLoop Loop
The Do While
Loop loop: Keeps looping while a loop conditionis True; Tests the condition at thestartofeach cycle.
Halts when the conditionbecomes False.
The VB syntax for the Do While
Loop loop is:
Do While conditionstatements
Loop
Keyword Do says, start the cycle
Keyword While says test ourcondition:
Tested at the start of each loop
If condition True, statementsare executed once;
Example: (sum < 1000)
Keyword Loop then tells control to loop (return to Do) And another cycle begins
-
8/14/2019 Lecture 7 - Loop Structures
10/20
The Do UntilLoop Loop
The Do Until
Loop loop: Keeps looping while a loop conditionis False; Tests the condition at thestartofeach cycle.
Halts after the cycle in which the conditionbecomes True.
The VB syntax for the Do Until
Loop loop is:
Do Until conditionstatements
Loop
Keyword Do says, start the cycle
Keyword Until says test ourcondition:
Tested at the start of each loop
If condition False, statementsare executed once;
Example: (sum >= 1000)
Keyword Loop then tells control to loop (return to Do). And anther cycle begins.
-
8/14/2019 Lecture 7 - Loop Structures
11/20
The DoLoop While Loop
The Do
Loop While loop: Keeps looping while a loop conditionis True; Tests the condition at the endofeach cycle.
Halts after the cycle in which the conditionbecomes False.
The VB syntax for the Do
Loop While loop is:Do
statementsLoop While condition
Keyword Do says: Execute our statements once.
Keywords Loop While say test our condition:
Tested at the bottom of each loop If condition True, control returns to
Do And another cycle begins.
Otherwise, the loop ends.
Example: (sum < 1000)
-
8/14/2019 Lecture 7 - Loop Structures
12/20
The Do Loop Until Loop
The Do
Loop Until loop: Keeps looping while a loop conditionis False; Tests the condition at the endofeach cycle.
Halts after the cycle in which the conditionbecomes True.
The VB syntax for the Do
Loop Until loop is:Do
statementsLoop Until condition
Keyword Do says: Execute our statements once.
Keywords Loop Until say test our condition:
Tested at the bottom of each loop If condition False, control returns to
Do And another cycle begins.
Otherwise, the loop ends.
Example: (sum >= 1000)
-
8/14/2019 Lecture 7 - Loop Structures
13/20
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 ForNext loop Purpose: Display all pairs of positive integers, each
-
8/14/2019 Lecture 7 - Loop Structures
14/20
-
8/14/2019 Lecture 7 - Loop Structures
15/20
Program to Demonstrate Loops
-
8/14/2019 Lecture 7 - Loop Structures
16/20
Program (cont.)
-
8/14/2019 Lecture 7 - Loop Structures
17/20
Program (cont.)
-
8/14/2019 Lecture 7 - Loop Structures
18/20
Program (cont.)
P ( t )
-
8/14/2019 Lecture 7 - Loop Structures
19/20
Program (cont.)
-
8/14/2019 Lecture 7 - Loop Structures
20/20