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.
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. 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.
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.
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.