12/15/2020
Repetition / Looping
Information Technology IIA
Lecture 05
By D.P Kalansuriya
A repetition structure allows the programmer to that an action is
to be repeated until given condition is true.
1
12/15/2020
VB Repetition
• Visual Basic repetition or looping statements
• Do While
• Repeat/Loop while true
• Often called pretest loop (also has posttest form)
• Do Until
• Repeat/Loop until true
• Often called posttest loop (also has pretest form)
• For...Next
• Repeat/Loop a specified number of times
Do While Looping Structure
• Do While loop: repeat while test expression is true
• The test expression is evaluated (pretest)
• When true, the statement(s)are executed
• The previous 2 steps are repeated as long
as the expression
evaluates to true
• When false, the
loop ends and True
expression
the statement(s) statement(s)
are not executed
False
2
12/15/2020
Do While Statement Syntax (pretest)
Do While expression
statement(s)
Loop
• Syntax explanation:
• Do, While, and Loop are keywords
• Do While statement marks the beginning of the loop and the Loop statement
marks the end
• The statement(s) to repeat are found between these and called the body of
the loop
• Expression – True/False value, variable
Do While Examples
Dim counter As Integer
counter = 1
Do While counter <= 3
Print counter
counter = counter + 1
Loop
3
12/15/2020
Infinite Loops
• A loop must have some way to end itself
• Something within the body of the loop must eventually force the test
expression to false
• In the previous example
• The loop continues to repeat
• counter increases by one for each repetition
• Finally (counter <= 3) is false and the loop ends
• If the test expression can never be false, the loop will continue to
repeat forever
• This is called an infinite loop
Controlling Loops
• Counter-controlled loops
• repeat a specific number of times
see counter from the previous example
• Counters are typically initialized before the loop begins
counter = 1
• The counter is then modified in the body of the loop
counter = counter + 1
4
12/15/2020
Counters and Accumulators
• Counter
• A numeric variable that keeps track of or counts: number of loop repetitions,
items that have been processed, or some other occurrence within a loop
see counter from the previous example
• Accumulator
• A numeric variable that is used to hold a sub-total that is computed during
multiple passes through a loop
Do Until Looping Structure
• Do Until loop: repeat until test expression is true
• The statement(s)are executed
• The test expression is
evaluated (posttest)
statement(s)
• When false, the previous 2
steps are repeated until the
expression evaluates to true
• When true, the loop ends and
the statement(s) are not False
executed again expression
True
5
12/15/2020
Do Until Statement Syntax (posttest)
Do
statement(s)
Loop Until expression
• Syntax explanation:
• Do, Loop, and Until are keywords
• Do statement marks the beginning of the loop and the Loop Until statement
marks the end
• The statement(s) to be repeated are found between these and called the
body of the loop
• Expression – True/False value, variable
Do Until Examples
Dim counter As Integer
counter = 3
Do
Print counter
counter = counter + 1
Loop Until counter > 3
6
12/15/2020
Pretest vs. Posttest Loops
• Previous Do While loops are in pretest form
• The expression is evaluated first
• Then the loop body is executed only if expression is true
• The body of a pretest loop may not be executed at all (if initial value of
expression is false)
• Previous Do Until loops are in posttest form
• The body of the loop is executed first
• Then the expression is evaluated
• The body of a posttest loop is executed at least once (even if initial value of
expression is true)
Do Until vs. Do While
• A Do While loop
• Repeats as long as its test expression is true
• Ends when its test expression becomes false
• Usually a pretest loop, Do
but also has a postest form: statement(s)
Loop While expression
• A Do Until loop
• Repeats as long as its test expression is false
• Ends when its test expression becomes true
• Usually a posttest loop, Do Until expression
but also has a pretest form:
statement(s)
Loop
7
12/15/2020
For Next Looping Structure
• For Next loop: used for counted loops that repeat a specific number of
times
• The Counter variable is set
Set
initially to the StartValue Counter
• After each loop iteration, to StartValue
the Step Value is added to Increment
the value of the counter Counter
variable. (if there is no step
value, 1 is added)
Counter > False
• Iteration continues until the EndValue?
statement(s)
EndValue is exceeded
True
For Next Statement Syntax (counted)
For Counter = StartValue To EndValue[Step Value]
statement[s]
Next [Counter]
• Syntax explanation:
• For, To, and Next are keywords
• Counter – Variable to track/control number of iterations
• StartValue is initial value of counter
• EndValue is counter value at final iteration
• Step (optional) – determines counter increment amount for each iteration of
the loop (if not specified the default is +1; if specified can be positive – add or
count up, or negative – subtract or count down
8
12/15/2020
For Next Examples
Dim counter As Integer
For counter = 1 To 10 Step 2
Print counter
Next
Dim counter As Integer
For counter = 10 To 1 Step -1
Print counter
Next
Exiting a Loop Prematurely
• In some cases it is convenient to end a loop before the test condition
would end it
• The following statements accomplish this
• Exit Do (used in Do While or Until loops)
• Exit For (used in For Next loops)
• Use this capability with caution
• It bypasses normal loop termination
• Makes code more difficult to debug
9
12/15/2020
Exiting a Loop Examples
Dim counter As Integer
For counter = 10 To 1 Step -1
If counter = 5 Then
Exit For
End If
Print counter
Next
Nested Loops
• The body of a loop can contain any type of VB statements including
another loop
• When a loop is found within the body of another loop, it’s called a
nested loop
10
12/15/2020
Nested Examples
Dim i, j As Integer
For i = 0 To 5
For j = 0 To 3
Print "*";
Next
Print '\n'
Next
11