Upload
p2arasan
View
234
Download
0
Embed Size (px)
Citation preview
8/6/2019 Iteration (Looping Constructs in VB)
1/13
Iteration (Looping Constructs in VB)Iteration: Groups of statements which are repeatedly
executed until a certain test is satisfied
Carrying out Iteration in VB programming, requires the
use ofLooping Constructs
There are 2 main types ofLooping Constructs:
Determinate Loops: will repeat themselves a known
(specific) number of times (For..Next Loops)
Indeterminate Loops: will repeat themselves an
unknown number of times (Do Loops) [Until; While]
8/6/2019 Iteration (Looping Constructs in VB)
2/13
A group of statement is repeated a specific number of
times
For..Next Loops
Repeat the statements in a loop a specific number oftimes
Each Forstatement has a corresponding Next
statement
For..Next Loop syntax:For Counter/Loop
Index = StartTo End [step step]
Statements (BodyofLoop)
NextCounter/LoopIndex
8/6/2019 Iteration (Looping Constructs in VB)
3/13
The For..Next loop uses the Forand Nextstatements
and a Counter Variable [LoopIndex]
The elements of a For..Next:The Counter/LoopIndex must be a numeric variable,
determines the number of times the statements inside
the loop will be executed
Start and End may be Constants, Variables, Numeric
Property Values, orNumeric Expressions, and further
determine the initialand finalvalue of the counter
The optional word Step may be included, along with the
value to be added to the LoopIndex(positive/negative)
for each iteration of the loop, and if omitted, the default
value is 1 for each increment of the loop
8/6/2019 Iteration (Looping Constructs in VB)
4/13
Dim iLoopIndex As Integer
Dim iMaximum As Integer
iMaximum = Inputbox(Enter the value, Number of
Entries)
ForiLoopIndex = 0 To iMaximum
The statements inside of the loop are indented, and
referred to as the body of the loop
Next iLoopIndex
8/6/2019 Iteration (Looping Constructs in VB)
5/13
ACounter-Controlled Loop generally has 3 elements:
Initialise the Counter
Increment the Counter[step]
Test the Counter to determine when it is time to
Terminate the loop
ForiIndex = 2 To 100 Step 2
will count from 2 to 100 by 2
The statements in the body of the loop will be executed
50 times, with iIndex = 2, 4, 6, .
The program checks forgreater than the test value and
not equal to
8/6/2019 Iteration (Looping Constructs in VB)
6/13
Exiting For..Next Loops
If you enter an Endless Loop, the program executionwill have to be broken manually
Therefore, you will need to enterBreak Time
Ctrl + Break
With For..Next loops, you may need to terminate the
loop before the loop index reaches its final value
VB provides an Exit For statement for this situation
Generally, an Exit Forstatement is part of an If
statement
8/6/2019 Iteration (Looping Constructs in VB)
7/13
ForiLoopIndex = 1 To 10
IftxtInput.Text = Then nothing was entered into the inputtextbox
MsgBox You must enter somethingExit For
End If
.
. statements in the loopNext iLoopIndex
8/6/2019 Iteration (Looping Constructs in VB)
8/13
8/6/2019 Iteration (Looping Constructs in VB)
9/13
8/6/2019 Iteration (Looping Constructs in VB)
10/13
8/6/2019 Iteration (Looping Constructs in VB)
11/13
8/6/2019 Iteration (Looping Constructs in VB)
12/13
8/6/2019 Iteration (Looping Constructs in VB)
13/13