Decision making allows a program to choose different actions based on specific conditions. In this VB.NET Decision Making Tutorial, you'll learn how to use the VB.NET If Then Statement, VB.NET If Else Statement, and VB.NET Select Case Statement to control the flow of your application and build interactive programs.
VB.NET If Then Statement
The VB.NET If Then Statement executes a block of code only when a condition evaluates to True. It is useful when you need to perform a single action based on a condition.
Dim age As Integer = 20
If age >= 18 Then
Console.WriteLine("Eligible to vote")
End If
Use this statement when you only need to check one condition.
VB.NET If Else Statement
The VB.NET If Else Statement executes one block of code when the condition is True and another when it is False.
Dim marks As Integer = 45
If marks >= 50 Then
Console.WriteLine("Pass")
Else
Console.WriteLine("Fail")
End If
If you're wondering how to use If Else in VB.NET, start by writing a Boolean condition inside the If statement. The program evaluates the condition and executes the appropriate block.
VB.NET Select Case Statement
The VB.NET Select Case Statement is a better choice when you need to compare one variable against multiple possible values. It makes your code cleaner than writing several ElseIf statements.
Dim grade As Char = "A"
Select Case grade
Case "A"
Console.WriteLine("Excellent")
Case "B"
Console.WriteLine("Good")
Case Else
Console.WriteLine("Needs Improvement")
End Select
This VB.NET Select Case example checks the value of grade and executes the matching case.
When Should You Use Each Statement?
Statement | Best Used For |
If Then | Checking a single condition |
If Else | Choosing between two outcomes |
Select Case | Comparing one variable with multiple values |
Best Practices
Keep conditions simple and easy to read.
Use Select Case instead of multiple ElseIf statements when testing one expression.
Avoid deeply nested conditions to improve readability.
Use meaningful variable names that clearly describe the condition being evaluated.
Leanring Visual Basic .NET Decision Making helps you build programs that respond intelligently to user input and different scenarios. Continue with the next tutorial on VB.NET Loops to learn how to execute a block of code repeatedly and automate repetitive tasks.
Frequently Asked Questions (FAQs)
Q: What is the purpose of the VB.NET If Then statement?
A: The If Then statement executes a block of code only when a condition evaluates to True. It is best used when you only need to check a single condition.
Q: When should you use the VB.NET If Else statement?
A: The If Else statement should be used when you need to choose between two outcomes, executing one block of code when the condition is True and another when it is False.
Q: Why is the VB.NET Select Case statement preferred over multiple ElseIf statements?
A: The Select Case statement is cleaner and easier to read when you need to compare a single variable against multiple possible values.