Compartilhar via


Como: Transferir controle para fora de uma estrutura de controle (Visual Basic)

Você pode usar o Declaração Saída (Visual Basic) para sair diretamente a partir de uma estruturade controle. Exittransfere a execução imediatamente para a demonstrativo após a última demonstrativo da estruturade controle. The syntax of the Exit statement specifies which type of control structure you are transferring out of. The following versions of the Exit statement are possible:

  • Exit Select

  • Exit Try

  • Exit While

  • Exit Do

  • Exit For

Exitinstruções podem aparecer quantas vezes for necessário, dentro de uma estrutura de controle que oferece suporte a eles. Exité útil quando uma estrutura de controle tem feito tudo o que ele precisa fazer e não precisa executar qualquer mais instruções.

Control Structures that Do Not Support Exit. Não é possível usar o Exitdedemonstrativo para transferir de um If, Using, ou With bloco. To achieve the same result, you can put a statement label on the block's End statement and transfer to it using a GoTo statement. For more information on statement labels, see Como: Rotular instruções (Visual Basic).

Exemplo

If an Exit statement is encountered within nested control structures, control passes to the statement following the end of the innermost structure of the kind specified in the Exit statement. The following example illustrates this.

Public Sub invertElements(ByRef a(,) As Double)
    For i As Integer = 0 To UBound(a, 1)
        For j As Integer = 0 To UBound(a, 2)
            If a(i, j) = 0 Then
                ' Cannot complete this row; resume outer loop.
                Exit For
            Else
                a(i, j) = 1.0 / a(i, j)
            End If
        Next j
        ' Control comes here directly from the Exit For statement.
    Next i
End Sub

In the preceding example, the Exit For statement is located in the inner For loop, so it passes control to the statement following that loop and continues with the outer For loop.

Consulte também

Tarefas

Como: Rotular instruções (Visual Basic)

Referência

Declaração Saída (Visual Basic)

Instrução GoTo

Conceitos

Estruturas de decisão (Visual Basic)

Estruturas de loop (Visual Basic)

Outras estruturas de controle (Visual Basic)

Estruturas de controle aninhado (Visual Basic)

Outros recursos

Fluxo de controle no Visual Basic