Wednesday, February 25, 2009

For...Next Structure

For...Next Structure
For...Next loop is used when you know how many times you want to execute loop.But in case of Do While it is not the case.
Do While is used when you do not know how many times you want to execute that particular loop/Condition.
Syntax:
For counter = start To end [Step increment]
statements
Next [counter]
The arguments counter, start, end, and increment are all numeric.
In executing the For loop, Visual Basic:
1. Sets counter equal to start.
2. Tests to see if counter is greater than end. If so, Visual Basic exits the loop.
3. Executes the statements.
4. Increments counter by 1 or as it's specified.
5. Repeats steps 2 through 4.
Example:
Dim A(40) as Integer
For i = 0 To 40 step 1
A(i)=i
Next
Print A(9)
Here index values are stored in array a(40).Then it prints the value of a(9).

No comments: