Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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 If...Then...Else Statement. 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 lets you evaluate an expression one time and run different sets of statements based on different possible values. For more information, see Select...Case Statement.
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 Try...Catch...Finally Statement.
Note
For many control structures, when you click a keyword, all of the keywords in the structure are highlighted. For instance, when you click If
in an If...Then...Else
construction, all instances of If
, Then
, ElseIf
, Else
, and End If
in the construction are highlighted. To move to the next or previous highlighted keyword, press CTRL+SHIFT+DOWN ARROW or CTRL+SHIFT+UP ARROW.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Add decision logic to your code using `if`, `else`, and `else if` statements in C# - Training
Learn to branch your code's execution path by evaluating Boolean expressions.
Documentation
Learn more about: Control Flow in Visual Basic
Other Control Structures - Visual Basic
Learn more about: Other Control Structures (Visual Basic)
Control Flow Summary - Visual Basic
Learn more about: Control Flow Summary (Visual Basic)