Estruturas de decisão (Visual Basic)
Visual Basic lets you test conditions and perform different operations depending on the results of that test. You can test for a condition being true or false, for various values of an expression, or for various exceptions generated when you execute a series of statements.
The following illustration shows a decision structure that tests for a condition being true and takes different actions depending on whether it is true or false.
Taking different actions when a condition is true and when it is false
If...Then...Else Construction
If...Then...Else constructions let you test for one or more conditions and run one or more statements depending on each condition. You can test conditions and take actions in the following ways:
Run one or more statements if a condition is True
Run one or more statements if a condition is False
Run some statements if a condition is True and others if it is False
Test an additional condition if a prior condition is False
The control structure that offers all these possibilities is the Declaração If...Then... (Visual Basic). You can use a single-line version if you have just one test and one statement to run. If you have a more complex set of conditions and actions, you can use the multiple-line version.
Select...Case Construction
The Select...Case construction lets you evaluate an expression one time and run different sets of statements based on different possible values. For more information, see Declaração Select...Case (Visual Basic).
Try...Catch...Finally Construction
Try...Catch...Finally constructions let you run a set of statements under an environment that retains control if any one of your statements causes an exception. You can take different actions for different exceptions. You can optionally specify a block of code that runs before you exit the whole Try...Catch...Finally construction, regardless of what occurs. For more information, see Instrução Try...Catch...Finally (Visual Basic).
Observação |
---|
For many control structures, when you click a keyword, all of the keywords in the structure are highlighted. Por exemplo, quando você clica em If em um If...Then...Else construção, todas as instâncias de If, Then, ElseIf, Else, e End If na construção são realçadas. Para mover para a realçado anterior ou seguinte palavra-chave, pressione CTRL + SHIFT + seta para baixo ou CTRL + SHIFT + seta para cima. For more information, see Como: Use o realce de referência. |
Consulte também
Tarefas
Como: Transferir controle para fora de uma estrutura de controle (Visual Basic)
Como: executar instruções dependendo de uma ou mais condições (Visual Basic)
Como: Testar vários valores de uma expressão (Visual Basic)
Como: Manter controle quando ocorre um erro (Visual Basic)
Conceitos
Estruturas de loop (Visual Basic)
Outras estruturas de controle (Visual Basic)
Estruturas de controle aninhado (Visual Basic)