Sponsored Links: |
Some basic information about various loop types.
..
.
.
.
.
.
.
.
.
.
.
.
.
.
' Loops allow you to run a group of statements repeatidly.
'
' There are four types of loop available, all very easy to
' use and understand. This code sample will explain how
' to use each type of loop.
'
' Do...Loop
' The Do...Loop will run a block of statements repeatidly
' while a condition is True, or until a condition becomes True
' Check these two examples of Do...While, there is one major difference
' between them. In Example A the cose will check the value of intCounter
' before it enters the loop, but in Example B the code will enter the
' loop regardless of the value of intCounter.
' Example A
intCounter = 0
Do While intCounter < 5
intCounter = intCounter + 1
MsgBox intCounter
Loop
' Example B
intCounter = 0
Do
intCounter = intCounter + 1
MsgBox intCounter
Loop While intCounter <5
' Here is the same examples using the Do...Until
' Example A
intCounter = 0
Do Until intCounter = 6
intCounter = intCounter + 1
MsgBox intCounter
Loop
' Example B
intCounter = 0
Do
intCounter = intCounter + 1
MsgBox intCounter
Loop Until intCounter = 6
' There are four types of loop available, all very easy to
' use and understand. This code sample will explain how
' to use each type of loop.
'
' Do...Loop
' The Do...Loop will run a block of statements repeatidly
' while a condition is True, or until a condition becomes True
' Check these two examples of Do...While, there is one major difference
' between them. In Example A the cose will check the value of intCounter
' before it enters the loop, but in Example B the code will enter the
' loop regardless of the value of intCounter.
' Example A
intCounter = 0
Do While intCounter < 5
intCounter = intCounter + 1
MsgBox intCounter
Loop
' Example B
intCounter = 0
Do
intCounter = intCounter + 1
MsgBox intCounter
Loop While intCounter <5
' Here is the same examples using the Do...Until
' Example A
intCounter = 0
Do Until intCounter = 6
intCounter = intCounter + 1
MsgBox intCounter
Loop
' Example B
intCounter = 0
Do
intCounter = intCounter + 1
MsgBox intCounter
Loop Until intCounter = 6
' For...Next
' For...Next Loops will execute a series of statements until a specific counter value
' is reached.
For iCounter = 1 To 5
MsgBox iCounter
Next
' You can add a Step keyword to define how much the counter should increase with each
' itteration of the loop
For iCounter = 1 To 10 Step 2
MsgBox iCounter
Next
' The Step keyword can also be used to itterate backwards
For iCounter = 5 to 1 Step -1
MsgBox iCounter
Next
' For...Each
' Another variation on the For...Next loop is the For...Each loop. The For...Each
' loop is used to execute a series of statements for each object in a collection,
' i.e. each element of an array. For example...
Dim strPeopleList
Dim strPerson
strPeopleList = Array("Alan", "Bob", "Craig", "Dan")
For Each strPerson in strPeopleList
MsgBox strPerson
Next
' While...Wend Loops
'
' This type of loop will execute a series of statements as long as
' a given condition is true.
' Note: It's advisable to avoid using this type of loop, you should
' us the Do...Loop instead
' Here's an example anyway...
iCounter = 0
While iCounter < 5
iCounter = iCounter + 1
Msgbox iCounter
Wend
Source: https://qtphelper.com
0 comments:
Post a Comment