typestar

If and ElseIf in Visual Basic

Block Ifs, one-line Ifs and the If() operator that picks a value.

Dim wind = 28

If wind > 40 Then
    Console.WriteLine("stay home")
ElseIf wind > 20 Then
    Console.WriteLine("great for kites")
Else
    Console.WriteLine("calm day")
End If

If wind Mod 2 = 0 Then Console.WriteLine("even reading")

Dim advice = If(wind > 20, "hold your hat", "bring a book")
Console.WriteLine(advice)
Console.WriteLine(If(wind < 5, "still", "breezy"))

How it works

  1. If/ElseIf/Else reads top down; blocks close with End If.
  2. A single-line If ... Then needs no End when the action fits.
  3. If(condition, a, b) is VB's ternary — an expression, not a statement.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
360
Tokens
98
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 62 seconds.

Type this snippet

Step 1 of 3 in Control flow, step 7 of 29 in Language basics.

← Previous Next →