Skip to content

If / ElseIf / Else / End If

Syntax

If <condition> Then
  ' statements when condition is True
ElseIf <otherCondition> Then
  ' statements when otherCondition is True
Else
  ' statements when none of the above are True
End If

Description

  • Evaluates <condition> (a Boolean expression).
  • If True, executes the first block.
  • ElseIf blocks allow multiple checks in sequence.
  • The optional Else block runs if no prior condition matched.
  • Always terminated with End If. (Except for single-line If-Then Statements (see example below))

Example

' Tiered If-Then/End If Statement
Dim score As Integer = 75

If score >= 90 Then
  Print("Grade: A")
ElseIf score >= 80 Then
  Print("Grade: B")
ElseIf score >= 70 Then
  Print("Grade: C")
ElseIf score >= 60 Then
  Print("Grade: D")
Else
  Print("Grade: F")
End If
' Single-line If-Then Statement
Dim x as Boolean = True
Dim i as Integer = 13

If x And i = 13 Then MessageBox("Values passed the condition)