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
If/ElseIf/Elsereads top down; blocks close withEnd If.- A single-line
If ... Thenneeds no End when the action fits. If(condition, a, b)is VB's ternary — an expression, not a statement.
Keywords and builtins used here
DimElseElseIfEndIfThen
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.
Step 1 of 3 in Control flow, step 7 of 29 in Language basics.