Control flow
Branching#
if / elif / else — if, else-if, else.
faber convert --to en — English reader surfacemain {
const int n ← 2
if n ≡ 1 {
print "one"
}
elif n ≡ 2 {
print "two"
}
else {
print "many"
}
}faber convert --to la — canonical Faberincipit {
fixum numerus n ← 2
si n ≡ 1 {
nota "one"
}
sin n ≡ 2 {
nota "two"
}
secus {
nota "many"
}
}faber convert --to th-TH — Thaiเริ่ม {
คงที่ จำนวน n ← 2
ถ้า n ≡ 1 {
บันทึก "one"
}
ถ้าไม่ก็ n ≡ 2 {
บันทึก "two"
}
มิฉะนั้น {
บันทึก "many"
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
常量 整数 n ← 2
如果 n ≡ 1 {
显示 "one"
}
否则如果 n ≡ 2 {
显示 "two"
}
否则 {
显示 "many"
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
定值 整數 n ← 2
若 n ≡ 1 {
註記 "one"
}
否則若 n ≡ 2 {
註記 "two"
}
否則 {
註記 "many"
}
}faber convert --to vi — Vietnamesebắt_đầu {
hằng số n ← 2
nếu n ≡ 1 {
ghi_chú "one"
}
nếukhôngthì n ≡ 2 {
ghi_chú "two"
}
khác {
ghi_chú "many"
}
}faber convert --to ar — Arabicبداية {
ثابت عدد n ← 2
إذا n ≡ 1 {
اعرض "one"
}
وإلاإذا n ≡ 2 {
اعرض "two"
}
وإلا {
اعرض "many"
}
}faber convert --to hi — Hindiआरंभ {
स्थिर संख्या n ← 2
यदि n ≡ 1 {
दिखाओ "one"
}
अन्यथायदि n ≡ 2 {
दिखाओ "two"
}
अन्यथा {
दिखाओ "many"
}
}One-line branch#
then takes a single statement instead of a block. Useful for guards.
faber convert --to en — English reader surfacefn abs(int n) → int {
if n ≺ 0 then return 0 - n
return n
}
main {
print abs(-5)
}faber convert --to la — canonical Faberfunctio abs(numerus n) → numerus {
si n ≺ 0 ergo redde 0 - n
redde n
}
incipit {
nota abs(-5)
}faber convert --to th-TH — Thaiฟังก์ชัน abs(จำนวน n) → จำนวน {
ถ้า n ≺ 0 ดังนั้น คืน 0 - n
คืน n
}
เริ่ม {
บันทึก abs(-5)
}faber convert --to zh-Hans — Simplified Chinese函数 abs(整数 n) → 整数 {
如果 n ≺ 0 则 返回 0 - n
返回 n
}
入口 {
显示 abs(-5)
}faber convert --to zh-Hant — Traditional Chinese函式 abs(整數 n) → 整數 {
若 n ≺ 0 則 傳回 0 - n
傳回 n
}
入口 {
註記 abs(-5)
}faber convert --to vi — Vietnamesehàm abs(số n) → số {
nếu n ≺ 0 do_đó trả 0 - n
trả n
}
bắt_đầu {
ghi_chú abs(-5)
}faber convert --to ar — Arabicدالة abs(عدد n) → عدد {
إذا n ≺ 0 إذن أعد 0 - n
أعد n
}
بداية {
اعرض abs(-5)
}faber convert --to hi — Hindiफलन abs(संख्या n) → संख्या {
यदि n ≺ 0 अतः लौटाओ 0 - n
लौटाओ n
}
आरंभ {
दिखाओ abs(-5)
}Selecting over a value#
switch picks an arm by value. default is the fallback.
faber convert --to en — English reader surfacemain {
const int n ← 2
switch n {
case 1 {
print "one"
}
case 2 {
print "two"
}
default {
print "many"
}
}
}faber convert --to la — canonical Faberincipit {
fixum numerus n ← 2
elige n {
casu 1 {
nota "one"
}
casu 2 {
nota "two"
}
ceterum {
nota "many"
}
}
}faber convert --to th-TH — Thaiเริ่ม {
คงที่ จํานวน n ← 2
เลือก n {
กรณี 1 {
บันทึก "one"
}
กรณี 2 {
บันทึก "two"
}
อื่น {
บันทึก "many"
}
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
常量 整数 n ← 2
选择 n {
情况 1 {
显示 "one"
}
情况 2 {
显示 "two"
}
默认 {
显示 "many"
}
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
定值 整數 n ← 2
選擇 n {
分支 1 {
註記 "one"
}
分支 2 {
註記 "two"
}
預設 {
註記 "many"
}
}
}faber convert --to vi — Vietnamesebắt_đầu {
hằng số n ← 2
chọn n {
trường_hợp 1 {
ghi_chú "one"
}
trường_hợp 2 {
ghi_chú "two"
}
mặc_định {
ghi_chú "many"
}
}
}faber convert --to ar — Arabicبداية {
ثابت عدد n ← 2
اختر n {
حالة 1 {
اعرض "one"
}
حالة 2 {
اعرض "two"
}
افتراضي {
اعرض "many"
}
}
}faber convert --to hi — Hindiआरंभ {
स्थिर संख्या n ← 2
चुनो n {
स्थिति 1 {
दिखाओ "one"
}
स्थिति 2 {
दिखाओ "two"
}
अन्यतम {
दिखाओ "many"
}
}
}Matching a union#
match matches over a union-typed value. This is how a
union hole gets read back out.
faber convert --to en — English reader surfacemain {
const string ∪ int signum ← 7
match signum {
case int const n {
print "a number"
}
case string const s {
print "a text"
}
}
}faber convert --to la — canonical Faberincipit {
fixum textus ∪ numerus signum ← 7
discerne signum {
casu numerus fixum n {
nota "a number"
}
casu textus fixum s {
nota "a text"
}
}
}faber convert --to th-TH — Thaiเริ่ม {
คงที่ ข้อความ ∪ จํานวน signum ← 7
แยก signum {
กรณี จํานวน คงที่ n {
บันทึก "a number"
}
กรณี ข้อความ คงที่ s {
บันทึก "a text"
}
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
常量 文本 ∪ 整数 signum ← 7
匹配 signum {
情况 整数 常量 n {
显示 "a number"
}
情况 文本 常量 s {
显示 "a text"
}
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
定值 文字 ∪ 整數 signum ← 7
比對 signum {
分支 整數 定值 n {
註記 "a number"
}
分支 文字 定值 s {
註記 "a text"
}
}
}faber convert --to vi — Vietnamesebắt_đầu {
hằng văn_bản ∪ số signum ← 7
phân_tích signum {
trường_hợp số hằng n {
ghi_chú "a number"
}
trường_hợp văn_bản hằng s {
ghi_chú "a text"
}
}
}faber convert --to ar — Arabicبداية {
ثابت نص ∪ عدد signum ← 7
طابق signum {
حالة عدد ثابت n {
اعرض "a number"
}
حالة نص ثابت s {
اعرض "a text"
}
}
}faber convert --to hi — Hindiआरंभ {
स्थिर पाठ ∪ संख्या signum ← 7
मिलाओ signum {
स्थिति संख्या स्थिर n {
दिखाओ "a number"
}
स्थिति पाठ स्थिर s {
दिखाओ "a text"
}
}
}The difference from switch is intent: switch chooses among values of one
type, match discriminates among the members of a union.
Returning#
return returns a value. A function with no → returns nothing and needs no
return.
fn duplica(int n) → int {
return n * 2
}
fn saluta(string name) → void {
print name
}
main {
print duplica(21)
saluta("Marcus")
}To exit by the error channel instead, see Errors and catching.
Related: Loops · Functions and flow