Exit Statement

 

Exits a block of Do...Loop, For...Next, Function, or Sub code.

Syntax

Exit Do
Exit For
Exit Function
Exit Property
Exit Sub

Remarks

The Exit statement syntax has these forms:

Statement

Description

Exit Do

Provides a way to exit a Do...Loop statement. It can be used only inside a Do...Loop statement. Exit Do transfers control to the statement following the Loop statement. When used within nested Do...Loop statements, Exit Do transfers control to the loop that is one nested level above the loop where it occurs.

Exit For

Provides a way to exit a For loop. It can be used only in a For...Next or For Each...Next loop. Exit For transfers control to the statement following the Next statement. When used within nested For loops, Exit For exits the current loop.

Exit Function

Immediately exits the Function procedure in which it appears. Execution continues with the statement following the statement that called the Function.

Exit Property

Immediately exits the Property procedure in which it appears. Execution continues with the statement following the statement that called the Property procedure.

Exit Sub

Immediately exits the Sub procedure in which it appears. Execution continues with the statement following the statement that called the Sub.

The following example illustrates the use of the Exit For statement:

Dim indexA, indexB

For indexA = 1 to 2
    For indexB = 1 to 100
        If indexB > 5 Then
            Exit For
        End If
        document.write (indexB & " ")
    Next
Next

' Output: 1 2 3 4 5 1 2 3 4 5

Requirements

Version 1

Change History

Date

History

Reason

September 2009

Modified the example.

Information enhancement.

September 2009

Modified the description for the Exit For statement.

Customer feedback.

See Also

Do...Loop Statement
For Each...Next Statement
For...Next Statement
Function Statement (VBScript)
Sub Statement