Control Flow in VB.NET
Control flow statements are essential for directing the execution path of your VB.NET programs. They allow you to make decisions, repeat code blocks, and manage the sequence of operations.
Conditional Statements
Conditional statements execute different blocks of code based on whether a specified condition is true or false.
If...Then...Else
Statement
The If...Then...Else
statement is the most common way to implement conditional logic.
If condition1 Then
' Code to execute if condition1 is true
ElseIf condition2 Then
' Code to execute if condition2 is true
Else
' Code to execute if none of the above conditions are true
End If
Select Case
Statement
The Select Case
statement allows you to select one of many code blocks to execute based on the value of an expression. It's often more readable than a long chain of If...ElseIf
statements.
Select Case expression
Case value1
' Code to execute if expression equals value1
Case value2, value3
' Code to execute if expression equals value2 or value3
Case Else
' Code to execute if expression does not match any of the above cases
End Select
Looping Statements
Looping statements allow you to execute a block of code repeatedly.
For...Next
Loop
The For...Next
loop iterates a specified number of times.
For counter As Integer = 1 To 10
' Code to execute in each iteration
Console.WriteLine(counter)
Next
' With a step value
For counter As Integer = 0 To 10 Step 2
Console.WriteLine(counter) ' Prints 0, 2, 4, 6, 8, 10
Next
Do...Loop
Statements
Do...Loop
statements repeat a block of code while a condition is true or until a condition becomes true.
' Do While loop
Dim i As Integer = 0
Do While i < 5
Console.WriteLine(i)
i += 1
Loop
' Do Until loop
Dim j As Integer = 0
Do Until j = 5
Console.WriteLine(j)
j += 1
Loop
' Do...Loop While
Dim k As Integer = 0
Do
Console.WriteLine(k)
k += 1
Loop While k < 5
' Do...Loop Until
Dim l As Integer = 0
Do
Console.WriteLine(l)
l += 1
Loop Until l = 5
For Each...Next
Loop
The For Each...Next
loop iterates over elements in a collection.
Dim numbers() As Integer = {1, 2, 3, 4, 5}
For Each num As Integer In numbers
Console.WriteLine(num)
Next
Branching Statements
Branching statements alter the normal sequence of program execution.
GoTo
Statement
The GoTo
statement transfers control to another line within the same procedure. While it exists, its use is generally discouraged due to its potential to make code hard to read and maintain.
Sub MySub()
Console.WriteLine("Start")
GoTo MyLabel
Console.WriteLine("This line will not be executed")
MyLabel:
Console.WriteLine("End")
End Sub
Break
and Continue
(within loops)
VB.NET does not have explicit break
or continue
keywords like some other languages. Instead, the functionality is achieved using the Exit
statement and by controlling loop conditions.
To achieve break
behavior (exit a loop):
For i As Integer = 1 To 10
If i = 5 Then
Exit For ' Exits the For loop
End If
Console.WriteLine(i)
Next
To achieve continue
behavior (skip the rest of the current iteration and move to the next):
For i As Integer = 1 To 10
If i Mod 2 = 0 Then ' Skip even numbers
' To simulate 'continue', we can simply not execute the rest of the code
' or use techniques like moving the rest of the code into an If block
' that is only executed when the condition is false.
' A direct 'continue' equivalent is not available.
Continue For ' Note: VB.NET does NOT have 'Continue For' or 'Continue Do'
' You must structure your code to skip the rest of the block.
' The following is a demonstration of the *intent* of continue.
End If
Console.WriteLine(i) ' This will only print odd numbers
Next
' Correct way to skip specific iterations in VB.NET:
For i As Integer = 1 To 10
If i Mod 2 = 0 Then
' Instead of 'continue', we skip the print statement for even numbers
Else
Console.WriteLine(i) ' Print odd numbers
End If
Next
The Exit Sub
, Exit Function
, Exit Property
, Exit Try
, and Exit Select
statements are also used to exit their respective constructs.
Error Handling
Structured exception handling is crucial for robust applications.
Try...Catch...Finally
Statement
This structure allows you to gracefully handle exceptions that occur during the execution of your code.
Try
' Code that might throw an exception
Dim result As Integer = 10 / 0
Catch ex As DivideByZeroException
' Handle the specific exception
Console.WriteLine("Error: Cannot divide by zero. " & ex.Message)
Catch ex As Exception
' Handle any other type of exception
Console.WriteLine("An unexpected error occurred: " & ex.Message)
Finally
' Code that will always execute, whether an exception occurred or not
Console.WriteLine("Execution finished.")
End Try