Basics, 5 of 8
Making decisions (If, Select)
Run code only when a condition holds, with If, ElseIf, and Else, or pick a case with Select.
What you’ll build
A shipping calculator: given a parcel’s weight and where it’s going, it works out the price and
how long delivery takes. Its decisions use If, which tests conditions in turn, and Select,
which compares one value against several cases.
If
If runs a block when its condition is True. ElseIf tests another condition when the ones
before it were False, and Else runs when none was True. Only the first branch whose
condition holds runs.
Import std.core
Function Price(grams: Int): Int
If grams <= 0
Return 0
ElseIf grams <= 500
Return 450
ElseIf grams <= 2000
Return 900
Else
Return 900 + (grams - 2000) / 1000 * 300
End If
End Function
For grams In [250, 1500, 2000, 5000]
Print("{grams} g: {Price(grams)} cents")
End For250 g: 450 cents 1500 g: 900 cents 2000 g: 900 cents 5000 g: 1800 cents
One line
When the code to run is a single statement, If ... Then ... fits on one line, with an optional
Else. This is the only form of If that uses Then.
Import std.core
Local grams = 2600
Local fragile = True
If fragile Then Print("handle with care")
If grams > 2000 Then Print("heavy parcel") Else Print("standard parcel")handle with care heavy parcel
Select
Select compares one value with each Case in turn and runs the first that matches. A case can
list several values, or a range with To, and Default runs when no case matches. Only one case
runs; there’s no falling through to the next.
Import std.core
Function DeliveryDays(zone: Int): String
Select zone
Case 1
Return "1 day"
Case 2, 3
Return "2 days"
Case 4 To 7
Return "3 to 5 days"
Default
Return "up to 2 weeks"
End Select
End Function
For zone In [1, 3, 5, 9]
Print("zone {zone}: {DeliveryDays(zone)}")
End Forzone 1: 1 day zone 3: 2 days zone 5: 3 to 5 days zone 9: up to 2 weeks
Each Case value must be known when the program is compiled, and two cases can’t match the same
value: that’s an error, so the order of the cases never hides one.
Strings and characters
Select also works on Strings, Chars, and Bools. Here a parcel’s destination
is a country code, a String, and its service is a one-letter code, a Char: E for express.
Import std.core
Function Zone(country: String): Int
Select country
Case "FR"
Return 1
Case "DE", "BE", "NL"
Return 2
Case "US", "CA"
Return 5
Default
Return 9
End Select
End Function
Function Service(code: Char): String
Select code
Case 'E'
Return "express"
Case 'A' To 'D'
Return "standard"
Default
Return "economy"
End Select
End Function
Print(Zone("BE"), Zone("CA"), Zone("JP"))
Print(Service('E'), Service('B'), Service('X'))2 5 9 express standard economy
A Select over an Enum must cover every member or have a Default, so adding a member later
shows each place that has to handle it. Enums shows this, and the kind of
Enum that always needs a Default.
What you learned
If,ElseIf, andElserun the first branch whose condition isTrue.If condition Then statementis the one-line form, with an optionalElse.Selectruns the firstCasethat matches its value, with no falling through.- A
Casecan list values or a range withTo, andDefaulttakes the rest. Selectworks on integers, Strings, Chars, Bools, and Enums.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.