CONTROL STRUCTURES
Control structures act like a "traffic director" for your code, deciding what happens next based on
conditions or rules
For example:
If it’s raining, take an umbrella.
Repeat this task 5 times.
In VB, these structures help your program respond to different situations, like user input or
changing data.
Types of Control Structures
There are three main types of control structures in VB:
1. Selection Structures (Making Choices)
If...Then: Do something if a condition is true.
Example
If temperature > 30 Then
[Link]("Take Umbrella!")
End If
If...Then...Else : Choose between two options.
Example
If score >= 50 Then
[Link]("Pass!")
Else
[Link]("Try again.")
End If
Select Case : Choose from multiple options
Example
Select Case dayOfWeek
Case "Monday"
[Link]("Start the week!")
Case "Friday"
[Link]("Celebrate the weekend!")
Case Else
[Link]("Normal day.")
End Select
2. Iteration Structures (Repeating Actions)
These make your program repeat tasks.
For...Next: Repeat a set number of times.
Example
For i = 1 To 5
[Link]("Count: " & i)
Next
Counts from 1 to 5 automatically
Do...Loop : Repeat while or until a condition is met.
Example
Do While password <> "secret"
[Link]("Incorrect password. Try again!")
Loop
Keeps asking for a password until it’s correct
While...End While: Similar to Do...Loop, but checks the condition first.
Example
While playerHealth > 0
[Link]("Keep playing!")
End While
Stops the game when health runs out.