Selection Structure
• Used when the program needs to make a decision.
• A condition is checked (True or False).
Types of Selection Structures
1. Single-Alternative
o Code runs only if the condition is true.
2. Dual-Alternative
o If the condition is true → run one set of code.
o If the condition is false → run another set.
Syntax
• If...Then...Else...End If
o Used for both single and dual alternatives.
o Contains blocks of code for each possible outcome.
Example:
If score >= 50 Then
[Link]("Passed")
Else
[Link]("Failed")
End If
Logical Operators
Used to combine multiple conditions.
Operator Description
Not Reverses the condition (True becomes False)
And Both conditions must be True
AndAlso Same as And, but skips second check if first is False (faster)
Or One or both conditions must be True
OrElse Same as Or, but skips second check if first is True (faster)
Xor True only if one of the conditions is True
Comparison Operators
Operator Description
Not Reverses the condition (True becomes False)
And Both conditions must be True
AndAlso Same as And, but skips second check if first is False (faster)
Or One or both conditions must be True
OrElse Same as Or, but skips second check if first is True (faster)
Xor True only if one of the conditions is True
Example of If statement with else if
Dim grade As Integer = 75
If grade >= 80 Then
[Link]("Excellent")
ElseIf grade >= 60 Then
[Link]("Good Job")
ElseIf grade >= 50 Then
[Link]("Passed")
Else
[Link]("Failed")
End If
Explanation:
• If the grade is 80 or more → shows "Excellent"
• If it’s 60 or more → shows "Good Job"
• If it’s 50 or more → shows "Passed"
• Otherwise → shows "Failed" that means the person got less than 50%
1. And Operator
True only if both conditions are True
Dim age As Integer = 20
Dim hasID As Boolean = True
If age >= 18 And hasID = True Then
[Link]("Allowed entry")
Else
[Link]("Entry denied")
End If
Output: "Allowed entry" because both are True.
2. AndAlso Operator
• Works like And but faster — stops checking if first is False
Dim age As Integer = 16
Dim hasID As Boolean = True
If age >= 18 AndAlso hasID = True Then
[Link]("Allowed entry")
Else
[Link]("Entry denied")
End If
Output: "Entry denied" — age is not ≥ 18, so it doesn't check hasID
3. Or Operator
• True if at least one condition is True
Dim isStudent As Boolean = True
Dim hasDiscountCard As Boolean = False
If isStudent Or hasDiscountCard Then
[Link]("You get a discount")
Else
[Link]("No discount")
End If
Output: "You get a discount" because isStudent is True.
4. OrElse Operator
• Same as Or, but skips checking second if first is True
Dim isStudent As Boolean = True
Dim hasDiscountCard As Boolean = True
If isStudent OrElse hasDiscountCard Then
[Link]("Discount applied")
Else
[Link]("No discount")
End If
Output: "Discount applied" — OrElse stops at the first True.
5. Not Operator
• Reverses the condition
Dim isLoggedIn As Boolean = False
If Not isLoggedIn Then
[Link]("Please log in")
Else
[Link]("Welcome back")
End If
Output: "Please log in" because Not False = True.
6. Xor Operator
• True if only one condition is True
Dim hasCoupon As Boolean = True
Dim isMember As Boolean = False
If hasCoupon Xor isMember Then
[Link]("Partial discount")
Else
[Link]("No or Full discount")
End If
Output: "Partial discount" — one is True, one is False.
If both are True or both are False → Xor returns False.