0% found this document useful (0 votes)
40 views23 pages

Ch3 PII

This document discusses loop structures and arrays in VB.NET. It covers the For...Next loop, which executes a block of statements a fixed number of times, and the Do...Loop structure, which allows repeating a block until a condition is met. Nested and infinite loops are also described. The document defines arrays as fixed-size collections of elements of the same type that can be individually accessed via an index. Examples show how to declare and use one-dimensional and two-dimensional arrays. An exercise provides code to calculate the average weekly rainfall across five districts using arrays.

Uploaded by

speedystories97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views23 pages

Ch3 PII

This document discusses loop structures and arrays in VB.NET. It covers the For...Next loop, which executes a block of statements a fixed number of times, and the Do...Loop structure, which allows repeating a block until a condition is met. Nested and infinite loops are also described. The document defines arrays as fixed-size collections of elements of the same type that can be individually accessed via an index. Examples show how to declare and use one-dimensional and two-dimensional arrays. An exercise provides code to calculate the average weekly rainfall across five districts using arrays.

Uploaded by

speedystories97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CHAPTER 03 (Part II)

1
Loop Structures

• Basic’s loop structures control how many


times a block of statements is executed.

• You can accomplish “looping” in one of two


ways : by executing a block of statements a
fixed number of times or by executing the
block repeatedly until a specified condition is
met.

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:

For Counter = Start To Stop


.. . Statement block. . .
Next Counter

Counter : A numeric variable that can be any type


Start & Stop : Values that specify the start & stop of the loop
3
For… Next (contd.)
Steps involved in For… Next loop structure:

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.

2. Statements in the loop are executed.

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.

• In its simplest form, a Do… Loop executes as long as a condition is True.

• Syntax is as follows:

Do While Condition
.. . Statement block. . .
Loop

7
Do… Loop Vs For… Next

8
Do… Loop (Exercise)

Write a program using do while loop to display all


numbers which are divided by 5 within the range of
1 to 50.

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:

Do While X > 0 Do While X > 0


For I = 1 to 10 For I = 1 to 10

.. . 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.)

• To declare an array in VB.Net,

Dim intData(30) ‘ an array of 31 elements


Dim strData(20) As String ‘ an array of 21 strings

Dim twoDarray(10,20) As Integer ‘ a twoD array of integers

• 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

You might also like