Control flow
Branching#
if / elif / else — if, else-if, else.
faber format --locale en — English reader surfacemain {
const int n ← 2
if n ≡ 1 {
print "one"
}
elif n ≡ 2 {
print "two"
}
else {
print "many"
}
}faber format --locale la — canonical Faberincipit {
fixum numerus n ← 2
si n ≡ 1 {
nota "one"
}
sin n ≡ 2 {
nota "two"
}
secus {
nota "many"
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ จำนวน n ← 2
ถ้า n ≡ 1 {
บันทึก "one"
}
ถ้าไม่ก็ n ≡ 2 {
บันทึก "two"
}
มิฉะนั้น {
บันทึก "many"
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 整数 n ← 2
如果 n ≡ 1 {
显示 "one"
}
否则如果 n ≡ 2 {
显示 "two"
}
否则 {
显示 "many"
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 整數 n ← 2
若 n ≡ 1 {
註記 "one"
}
否則若 n ≡ 2 {
註記 "two"
}
否則 {
註記 "many"
}
}faber format --locale 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 format --locale ar — Arabicبداية {
ثابت عدد n ← 2
إذا n ≡ 1 {
اعرض "one"
}
وإلاإذا n ≡ 2 {
اعرض "two"
}
وإلا {
اعرض "many"
}
}faber format --locale hi — Hindiआरंभ {
स्थिर संख्या n ← 2
यदि n ≡ 1 {
दिखाओ "one"
}
अन्यथायदि n ≡ 2 {
दिखाओ "two"
}
अन्यथा {
दिखाओ "many"
}
}One-line branch#
ergo takes a single statement instead of a block. Useful for guards.
faber format --locale en — English reader surfacefn abs(int n) → int {
if n ≺ 0 then return 0 - n
return n
}
main {
print abs(-5)
}faber format --locale la — canonical Faberfunctio abs(numerus n) → numerus {
si n ≺ 0 ergo redde 0 - n
redde n
}
incipit {
nota abs(-5)
}faber format --locale th-TH — Thaiฟังก์ชัน abs(จำนวน n) → จำนวน {
ถ้า n ≺ 0 ดังนั้น คืน 0 - n
คืน n
}
เริ่ม {
บันทึก abs(-5)
}faber format --locale zh-Hans — Simplified Chinese函数 abs(整数 n) → 整数 {
如果 n ≺ 0 则 返回 0 - n
返回 n
}
入口 {
显示 abs(-5)
}faber format --locale zh-Hant — Traditional Chinese函式 abs(整數 n) → 整數 {
若 n ≺ 0 則 傳回 0 - n
傳回 n
}
入口 {
註記 abs(-5)
}faber format --locale 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 format --locale ar — Arabicدالة abs(عدد n) → عدد {
إذا n ≺ 0 إذن أعد 0 - n
أعد n
}
بداية {
اعرض abs(-5)
}faber format --locale 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 format --locale en — English reader surfacemain {
const int n ← 2
match n {
case 1 {
print "one"
}
case 2 {
print "two"
}
case _ {
print "many"
}
}
}faber format --locale la — canonical Faberincipit {
fixum numerus n ← 2
discerne n {
casu 1 {
nota "one"
}
casu 2 {
nota "two"
}
casu _ {
nota "many"
}
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ จำนวน n ← 2
แยก n {
กรณี 1 {
บันทึก "one"
}
กรณี 2 {
บันทึก "two"
}
กรณี _ {
บันทึก "many"
}
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 整数 n ← 2
匹配 n {
情况 1 {
显示 "one"
}
情况 2 {
显示 "two"
}
情况 _ {
显示 "many"
}
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 整數 n ← 2
比對 n {
分支 1 {
註記 "one"
}
分支 2 {
註記 "two"
}
分支 _ {
註記 "many"
}
}
}faber format --locale vi — Vietnamesebắt_đầu {
hằng số n ← 2
phân_tích n {
trường_hợp 1 {
ghi_chú "one"
}
trường_hợp 2 {
ghi_chú "two"
}
trường_hợp _ {
ghi_chú "many"
}
}
}faber format --locale ar — Arabicبداية {
ثابت عدد n ← 2
طابق n {
حالة 1 {
اعرض "one"
}
حالة 2 {
اعرض "two"
}
حالة _ {
اعرض "many"
}
}
}faber format --locale 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 format --locale en — English reader surfacemain {
const string ∪ int signum ← 7
match signum {
case 7 {
print "the number seven"
}
case "x" {
print "the letter x"
}
case _ {
print "something else"
}
}
}faber format --locale la — canonical Faberincipit {
fixum textus ∪ numerus signum ← 7
discerne signum {
casu 7 {
nota "the number seven"
}
casu "x" {
nota "the letter x"
}
casu _ {
nota "something else"
}
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ ข้อความ ∪ จำนวน signum ← 7
แยก signum {
กรณี 7 {
บันทึก "the number seven"
}
กรณี "x" {
บันทึก "the letter x"
}
กรณี _ {
บันทึก "something else"
}
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 文本 ∪ 整数 signum ← 7
匹配 signum {
情况 7 {
显示 "the number seven"
}
情况 "x" {
显示 "the letter x"
}
情况 _ {
显示 "something else"
}
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 文字 ∪ 整數 signum ← 7
比對 signum {
分支 7 {
註記 "the number seven"
}
分支 "x" {
註記 "the letter x"
}
分支 _ {
註記 "something else"
}
}
}faber format --locale vi — Vietnamesebắt_đầu {
hằng văn_bản ∪ số signum ← 7
phân_tích signum {
trường_hợp 7 {
ghi_chú "the number seven"
}
trường_hợp "x" {
ghi_chú "the letter x"
}
trường_hợp _ {
ghi_chú "something else"
}
}
}faber format --locale ar — Arabicبداية {
ثابت نص ∪ عدد signum ← 7
طابق signum {
حالة 7 {
اعرض "the number seven"
}
حالة "x" {
اعرض "the letter x"
}
حالة _ {
اعرض "something else"
}
}
}faber format --locale hi — Hindiआरंभ {
स्थिर पाठ ∪ संख्या signum ← 7
मिलाओ signum {
स्थिति 7 {
दिखाओ "the number seven"
}
स्थिति "x" {
दिखाओ "the letter x"
}
स्थिति _ {
दिखाओ "something else"
}
}
}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.
faber format --locale en — English reader surfacefn duplica(int n) → int {
return n * 2
}
fn saluta(string nomen) → void {
print nomen
}
main {
print duplica(21)
saluta("Marcus")
}faber format --locale la — canonical Faberfunctio duplica(numerus n) → numerus {
redde n * 2
}
functio saluta(textus nomen) → vacuum {
nota nomen
}
incipit {
nota duplica(21)
saluta("Marcus")
}faber format --locale th-TH — Thaiฟังก์ชัน duplica(จำนวน n) → จำนวน {
คืน n * 2
}
ฟังก์ชัน saluta(ข้อความ nomen) → เปล่า {
บันทึก nomen
}
เริ่ม {
บันทึก duplica(21)
saluta("Marcus")
}faber format --locale zh-Hans — Simplified Chinese函数 duplica(整数 n) → 整数 {
返回 n * 2
}
函数 saluta(文本 nomen) → 无值 {
显示 nomen
}
入口 {
显示 duplica(21)
saluta("Marcus")
}faber format --locale zh-Hant — Traditional Chinese函式 duplica(整數 n) → 整數 {
傳回 n * 2
}
函式 saluta(文字 nomen) → 空值 {
註記 nomen
}
入口 {
註記 duplica(21)
saluta("Marcus")
}faber format --locale vi — Vietnamesehàm duplica(số n) → số {
trả n * 2
}
hàm saluta(văn_bản nomen) → trống {
ghi_chú nomen
}
bắt_đầu {
ghi_chú duplica(21)
saluta("Marcus")
}faber format --locale ar — Arabicدالة duplica(عدد n) → عدد {
أعد n * 2
}
دالة saluta(نص nomen) → فراغ {
اعرض nomen
}
بداية {
اعرض duplica(21)
saluta("Marcus")
}faber format --locale hi — Hindiफलन duplica(संख्या n) → संख्या {
लौटाओ n * 2
}
फलन saluta(पाठ nomen) → रिक्त {
दिखाओ nomen
}
आरंभ {
दिखाओ duplica(21)
saluta("Marcus")
}To exit by the error channel instead, see Errors and catching.
Related: Loops · Functions and flow