If then else
If...Then...Else
Conditionally executes a block of statements based on the value of a Boolean expression.
Syntax
' Block form
If condition Then
[statements]
[ElseIf condition2 Then
[statements]]
[Else
[statements]]
End If
' Single-line form
If condition Then statement [Else statement]
Parameters
- condition: A Boolean expression to evaluate.
- statements: One or more statements to execute when the condition is met.
Remarks
- Multiple
ElseIfbranches can appear betweenIfandElse. - Only the first branch whose condition is
Trueexecutes. - The
Elsebranch executes when none of the preceding conditions areTrue. - In the single-line form,
End Ifis not used and only one statement may followThenorElse.
Examples
Dim score As Integer
score = 75
If score >= 90 Then
Print "A"
ElseIf score >= 70 Then
Print "B"
ElseIf score >= 50 Then
Print "C"
Else
Print "F"
End If
' Output: B
' Single-line form
Dim x As Integer
x = 10
If x > 0 Then Print "Positive" Else Print "Non-positive"