Looping statements

DO LOOP
————–
Do loop has 2 version. one where condition is evaluated at the beginning
whose Syntax:

Do [while | until] [condition]
[statements]
[exit do]
[increment | statements]
Loop

or one where condition is evaluated at the end.
whose syntax

Do
[statements]
[exit do]
[increment | statements]
Loop [while | until] [condition]

For example checking end of file using EOF() function

Do until EOF(1)
line input #1, Data$
Form1.tb.text = Form1.tb.text + Data$
Loop

FOR LOOP

—————-

Do loop doesnot require loop index but for loop does. So used when number of iteration is known.

SYNTAX
For index = start to end increment [1 | -1]
[statements]
[Exit for]
[statements]
Next index

for example
Dim intindex, total
total = 0
For intindex = 1 to 10
total = total + 1
next intindex

Default incrementation is +1.

FOR EACH LOOP
Use is to loop over elements in an Array or Collection

For each element in group
[statements]
[exit for]
[statements]
next element

For example
Dim IArray(1 to 3)
IArray(1) = 1
IArray(2) = 2
IArray(3) = 3

For each Arrayitem in IArray
msgbox (Str(Arrayitem))
next Arrayitem

WHILE LOOP
used when we want to stop loop when condition is no longer true.

SYNTAX
while condition
[statements]
Wend

for example

Dim intinput
intinput =-1

While intinput<0
intinput = InputBox(“Enter positive number”)
Wend

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s