Professional Documents
Culture Documents
Ch3 PII
Ch3 PII
Ch3 PII
1
Loop Structures
2
For… Next : loop structure
• In its most common use, the For…Next loop executes a block of statements a
fixed number of times.
• Syntax is as follows:
1. Counter is compared with Stop. If counter is greater than stop, execution passes
to the first statement after the For…Next loop. If counter is not greater than
stop, go to Step 2.
3. Counter is incremented by 1.
4. Return to step 1.
4
For… Next (Example)
5
For…Step Next (Example)
6
Do… Loop : loop structure
• The Do… Loop structure is the most flexible of Visual Basic’s loops. It allows the
loop to execute repeatedly until a specified condition is either True or False, and
it allows the condition to be evaluated either at the start or the end of the loop.
• Syntax is as follows:
Do While Condition
.. . Statement block. . .
Loop
7
Do… Loop Vs For… Next
8
Do… Loop (Exercise)
9
Do… Loop (Exercise- Answer)
10
Nested and Infinite Loops
• A Nested loop is a loop contained within another loop.
• The only restriction on nesting loops is that each inner loop must be enclosed
entirely within the outer loop.
• Consider the following example:
.. . Statements. . . .. . Statements. . .
Loop Next I
Next I Loop
11
Arrays in VB .net
• An array stores a fixed-size sequential collection of elements of the same type.
• It can be individually referenced by adding an index to a unique name.
• For eg.
Let assume that, you are creating an array with the name of arr(4).
12
Arrays in VB .net(contd.)
• Dynamic arrays are arrays that can be dimensioned and re-dimensioned as per
the need of the program.
13
Arrays (Example 1)
14
Arrays (Example 2)
15
Arrays (Example 2)
16
Exercise
It is a weekly rainfall summarization system for particular five districts: Colombo, Kandy, Batticaloa,
Jaffna and Matara to calculate the average rainfall of the particular week.
17
Exercise
18
Exercise (Answer)
19
Exercise (Answer)
20
Exercise (Answer)
21
Exercise (Answer)
22
Exercise (Answer)
23