Making Decisions with If...Then...Else
VB.NET uses the If...Then...Else construct to branch program execution based on Boolean conditions. A single-line form works for simple checks, such as If age >= 18 Then Console.WriteLine("Adult"), while the block form spans multiple lines with ElseIf clauses for additional conditions and a final Else for the catch-all case, closed by End If. Each condition is evaluated top to bottom, and the first True branch executes while the rest are skipped.
Cricket analogy: A bowler decides the next delivery like an If...ElseIf chain: If the batsman is set and going for boundaries, bowl a yorker; ElseIf the pitch is turning, bowl a slower ball; Else stick to a stock delivery outside off stump.
Select Case for Multi-Way Branching
When a single variable must be compared against many possible values, Select Case...End Select is often clearer than a long ElseIf chain. Each Case clause can match a literal, a comma-separated list, a range using To, or a comparison using Is, and Case Else handles any value not explicitly covered. Unlike some languages, VB.NET's Select Case does not fall through between cases, so execution jumps out automatically once a matching case body finishes.
Cricket analogy: An umpire's Select Case on the ball's outcome routes to different signals: Case is a boundary rope contact, signal four; Case is over the rope without bouncing, signal six; Case Else, just call dead ball, with no signal falling through to the next.
Comparison and Logical Operators
VB.NET's comparison operators (=, <>, <, >, <=, >=) build the Boolean expressions that conditionals evaluate, while logical operators combine them: And and Or evaluate both sides fully, whereas AndAlso and OrElse short-circuit, skipping the right-hand expression once the result is already determined. This matters when the second operand has side effects or could throw, such as checking obj IsNot Nothing AndAlso obj.Value > 0, which safely avoids a NullReferenceException.
Cricket analogy: A DRS review uses short-circuit logic: If the on-field call was already Out AndAlso the ball-tracking shows clipping the stumps, the review confirms Out immediately without needing to check the snickometer for an inside edge that would no longer matter.
Nested Conditionals and Best Practices
Conditionals can be nested inside one another to handle layered decisions, but deep nesting quickly becomes hard to read and maintain. A common technique is to use guard clauses — early If checks that return or exit before the main logic — so the happy path isn't buried three or four indentation levels deep. Favoring Select Case over long ElseIf chains, and extracting complex Boolean expressions into well-named variables, also keeps conditional logic readable in larger VB.NET procedures.
Cricket analogy: A team's batting order strategy avoids over-nesting decisions: rather than nesting five conditions deep about pitch, weather, and opposition bowlers, a captain uses a simple guard clause — If rain is forecast, bat first, Return — before evaluating anything else.
Module ConditionalsDemo
Sub Main()
Dim score As Integer = 82
' If...ElseIf...Else
If score >= 90 Then
Console.WriteLine("Grade: A")
ElseIf score >= 80 Then
Console.WriteLine("Grade: B")
ElseIf score >= 70 Then
Console.WriteLine("Grade: C")
Else
Console.WriteLine("Grade: F")
End If
' Select Case with ranges
Select Case score
Case Is >= 90
Console.WriteLine("Excellent")
Case 80 To 89
Console.WriteLine("Good")
Case 70 To 79
Console.WriteLine("Average")
Case Else
Console.WriteLine("Needs improvement")
End Select
' Short-circuit evaluation with AndAlso
Dim student As Student = Nothing
If student IsNot Nothing AndAlso student.Score > 0 Then
Console.WriteLine("Valid student record")
End If
End Sub
End ModulePrefer AndAlso and OrElse over And and Or in conditional expressions. They short-circuit evaluation, which both improves performance by skipping unnecessary work and prevents runtime errors when a later expression depends on an earlier condition being true, such as a null check before a property access.
Unlike C-family languages, VB.NET's Select Case never falls through to the next Case automatically — each matched case's block runs and then control exits the Select Case entirely, so you never need (or can use) an Exit Select for that purpose, though it can still be used to break out early from within a case body.
- If...Then...Else evaluates conditions top to bottom and executes only the first True branch.
- Select Case is clearer than long ElseIf chains for multi-way branching on one variable, supporting literals, comma lists, To ranges, and Is comparisons.
- VB.NET's Select Case does not fall through between cases; each match runs exactly one block.
- AndAlso and OrElse short-circuit evaluation, avoiding unnecessary or unsafe evaluation of the right-hand operand.
- And and Or always evaluate both operands, which can hurt performance or cause errors like null reference exceptions.
- Guard clauses (early exits) reduce deep nesting and keep the main logic path readable.
- Extracting complex Boolean expressions into well-named variables improves readability of conditional code.
Practice what you learned
1. Which VB.NET operator provides short-circuit evaluation for a logical AND?
2. What happens when a Case in a Select Case block finishes executing in VB.NET?
3. Which Select Case clause matches any score of 90 or above?
4. Why might obj IsNot Nothing AndAlso obj.Value > 0 be safer than using And?
5. What is a 'guard clause' used for in conditional logic?
Was this page helpful?
You May Also Like
Loops in VB.NET
Master repetitive control flow in VB.NET using For...Next, While, Do loops, and For Each, including loop control statements like Exit and Continue.
Subs and Functions in VB.NET
Understand how to organize reusable code in VB.NET using Sub and Function procedures, including parameters, return values, and overloading.
Error Handling with Try/Catch
Learn structured exception handling in VB.NET using Try, Catch, Finally, and Throw to write robust code that gracefully handles runtime errors.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics