Partager via


Decision Structures 

Visual Basic allows you to 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

Flow chart of an If...Then...Else construction

If...Then...Else Construction

If...Then...Else constructions allow you to 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 If...Then...Else Statement (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 allows you to evaluate an expression once and run different sets of statements based on different possible values. For more information, see Select...Case Statement (Visual Basic).

Try...Catch...Finally Construction

Try...Catch...Finally constructions allow you to run a set of statements under an environment that retains control if any of your statements causes an exception. You can take different actions for different exceptions. You can optionally specify a block of code that is to run before you exit the entire Try...Catch...Finally construction, no matter what happens. For more information, see Try...Catch...Finally Statement (Visual Basic).

See Also

Tasks

How to: Transfer Control Out of a Control Structure
How to: Run Statements Depending on One or More Conditions
How to: Test for Several Values of an Expression
How to: Retain Control When an Error Occurs

Concepts

Loop Structures
Other Control Structures
Nested Control Structures

Other Resources

Control Flow in Visual Basic