Skip to content
If/Else and Select Case
step 1/5

Reading — step 1 of 5

Learn

~1 min readControl Flow

If/Then/ElseIf/Else/End If:

If age >= 18 Then
    Console.WriteLine("adult")
ElseIf age >= 13 Then
    Console.WriteLine("teen")
Else
    Console.WriteLine("child")
End If

Single-line form (no End If needed):

If x > 0 Then Console.WriteLine("positive")

Comparison operators: = (equal — same as assignment, context-disambiguated), <> (not equal), <, >, <=, >=.

Logical: And, Or, Not, AndAlso (short-circuit), OrElse (short-circuit). The AndAlso/OrElse are usually what you want — they don't evaluate the right side if the left determines the answer.

Select Case:

Select Case grade
    Case "A"
        Console.WriteLine("excellent")
    Case "B" To "D"
        Console.WriteLine("passing")
    Case Else
        Console.WriteLine("failed")
End Select

Powerful — supports Is >, To ranges, comma-separated lists.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…