Documente Academic
Documente Profesional
Documente Cultură
INTRODUCTION
Looping statements allow one to iterate through a code block multiple times. The
condition for looping through the code block depends on the type of looping statement. The
looping statement types are While End While , Do Loop , For Next , and For Each Next . As
with the branching statements, the basic syntax and operation of these Visual Basic .NET
statements mirror their counterparts in classic Visual Basic. The sole syntax change was made
to the former While Wend statement, which has been updated to While End While. Both the
Do Loop and While End While statements permit looping as long as a condition is True. In
addition, the Do Loop statement allows a code block to execute until a condition becomes
True.
One can pass control to the first statement following either type of looping statement
by using either an Exit While or Exit Do statement. The For Next statement allows one to
pass through code for a fixed number of iterations based on the arguments for the statement.
One can specify a value that increases or decreases by a fixed numeric increment up to or
down to a limit. When the value passes the limit, control transfers to the first statement after
the For Next loop. The For Each Next statement has a statement name similar to the For Next
statement, but its operation differs significantly. The For Each Next statement lets one pass
through the elements in a set, such as an array or a collection.
EXPLANATION
In a While..Wend loop, if the condition is True, all statements are executed until
Wend keyword is encountered. If the condition is false, the loop is exited and the control
jumps to very next statement after Wend keyword.
SYNTAX
While condition(s)
[statements 1]
1
[statements 2]
...
[statements n]
Wend
FLOW DIAGRAM
EXIT STATEMENT
Exit Do
Immediately exits the Do loop in which it appears. Execution continues with the
statement following the Loop statement. Exit Do can be used only inside a Do loop. When
used within nested Do loops, Exit Do exits the innermost loop and transfers control to the
next higher level of nesting.
2
Exit For
Immediately exits the For loop in which it appears. Execution continues with the
statement following the Next statement. Exit For can be used only inside a For...Next or For
Each...Next loop. When used within nested For loops, Exit For exits the innermost loop and
transfers control to the next higher level of nesting.
Exit Function
Exit Select
Immediately exits the Select Case block in which it appears. Execution continues with
the statement following the End Select statement. Exit Select can be used only inside a Select
Case statement.
Exit Sub
Immediately exits the Sub procedure in which it appears. Execution continues with
the statement following the statement that called the Sub procedure. Exit Sub can be used
only inside a Sub procedure. In a Sub procedure, the Exit Sub statement is equivalent to the
Return statement.
Exit While
Immediately exits the While loop in which it appears. Execution continues with the
statement following the End While statement. Exit While can be used only inside a While
loop. When used within nested While loops, Exit While transfers control to the loop that is
one nested level above the loop where Exit While occurs.
3
Exit Try
Immediately exits the Try or Catch block in which it appears. Execution continues
with the Finally block if there is one, or with the statement following the End Try statement
otherwise. Exit Try can be used only inside a Try or Catch block, and not inside a Finally
block.
Exit Property
EXAMPLE
index += 1
Continue While
End If
If index = 10 Then
Exit While
End If
4
End While
Debug.WriteLine("")
Output: 1 2 3 4 9 10
CONCLUSION
The key point is that the loop passes through the individual elements not the indexes
for the elements in a set. When one are working with the members of a set, the For Each
Next syntax will often be simpler than the For Next syntax. However, when one are not
iterating over the members of a set, it is helpful to have the For Next statement available.
Both types of For loops provide support for common looping features. For example, one can
invoke an Exit For statement to exit a loop before reaching its end. One can nest For loops
within one another. It is common to have an inner loop pass through the columns on a row
while an outer loop designates a succession of rows.
REFERENCES
http://www.scribd.com/doc/6866273/LESSON-3-Branching-and-Looping
http://flylib.com/books/en/2.279.1.26/1/
http://www.tutorialspoint.com/vbscript/vbscript_while_wend_loop.htm
https://msdn.microsoft.com/en-us/library/t2at9t47.aspx