Loops
for iterates. The word after it is the mode — what kind of traversal
this is — and it is required, so a loop always says what it walks.
| Mode | Walks |
|---|---|
from | the values of a collection |
ref | the keys of a table, or the indices of a list |
ab | a numeric range |
Over values#
faber format --locale en — English reader surfacemain {
const list<int> numeri ← [1, 2, 3]
for from numeri const n {
print n
}
}faber format --locale la — canonical Faberincipit {
fixum lista<numerus> numeri ← [1, 2, 3]
itera ex numeri fixum n {
nota n
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ รายการ<จำนวน> numeri ← [1, 2, 3]
วน ออก numeri คงที่ n {
บันทึก n
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 列表<整数> numeri ← [1, 2, 3]
遍历 取自 numeri 常量 n {
显示 n
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 列表<整數> numeri ← [1, 2, 3]
遍歷 取自 numeri 定值 n {
註記 n
}
}faber format --locale vi — Vietnamesebắt_đầu {
hằng danh_sách<số> numeri ← [1, 2, 3]
lặp từ numeri hằng n {
ghi_chú n
}
}faber format --locale ar — Arabicبداية {
ثابت قائمة<عدد> numeri ← [1, 2, 3]
كرر من numeri ثابت n {
اعرض n
}
}faber format --locale hi — Hindiआरंभ {
स्थिर सूची<संख्या> numeri ← [1, 2, 3]
दोहराओ सेवन numeri स्थिर n {
दिखाओ n
}
}Over keys#
faber format --locale en — English reader surfacemain {
const map<string, string> persona ← { "nomen": "Marcus", "urbs": "Roma" }
for ref persona const clavis {
print clavis, persona[clavis]
}
}faber format --locale la — canonical Faberincipit {
fixum tabula<textus, textus> persona ← { "nomen": "Marcus", "urbs": "Roma" }
itera de persona fixum clavis {
nota clavis, persona[clavis]
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ ตาราง<ข้อความ, ข้อความ> persona ← { "nomen": "Marcus", "urbs": "Roma" }
วน จาก persona คงที่ clavis {
บันทึก clavis, persona[clavis]
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 映射<文本, 文本> persona ← { "nomen": "Marcus", "urbs": "Roma" }
遍历 借自 persona 常量 clavis {
显示 clavis, persona[clavis]
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 表格<文字, 文字> persona ← { "nomen": "Marcus", "urbs": "Roma" }
遍歷 從 persona 定值 clavis {
註記 clavis, persona[clavis]
}
}faber format --locale vi — Vietnamesebắt_đầu {
hằng bảng<văn_bản, văn_bản> persona ← { "nomen": "Marcus", "urbs": "Roma" }
lặp ra persona hằng clavis {
ghi_chú clavis, persona[clavis]
}
}faber format --locale ar — Arabicبداية {
ثابت جدول<نص, نص> persona ← { "nomen": "Marcus", "urbs": "Roma" }
كرر عن persona ثابت clavis {
اعرض clavis, persona[clavis]
}
}faber format --locale hi — Hindiआरंभ {
स्थिर तालिका<पाठ, पाठ> persona ← { "nomen": "Marcus", "urbs": "Roma" }
दोहराओ से persona स्थिर clavis {
दिखाओ clavis, persona[clavis]
}
}The same mode gives you list indices, which is what you want when the position matters as much as the value:
faber format --locale en — English reader surfacemain {
const list<int> numeri ← [10, 20, 30]
for ref numeri const index {
print index, numeri[index]
}
}faber format --locale la — canonical Faberincipit {
fixum lista<numerus> numeri ← [10, 20, 30]
itera de numeri fixum index {
nota index, numeri[index]
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ รายการ<จำนวน> numeri ← [10, 20, 30]
วน จาก numeri คงที่ index {
บันทึก index, numeri[index]
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 列表<整数> numeri ← [10, 20, 30]
遍历 借自 numeri 常量 index {
显示 index, numeri[index]
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 列表<整數> numeri ← [10, 20, 30]
遍歷 從 numeri 定值 index {
註記 index, numeri[index]
}
}faber format --locale vi — Vietnamesebắt_đầu {
hằng danh_sách<số> numeri ← [10, 20, 30]
lặp ra numeri hằng index {
ghi_chú index, numeri[index]
}
}faber format --locale ar — Arabicبداية {
ثابت قائمة<عدد> numeri ← [10, 20, 30]
كرر عن numeri ثابت index {
اعرض index, numeri[index]
}
}faber format --locale hi — Hindiआरंभ {
स्थिर सूची<संख्या> numeri ← [10, 20, 30]
दोहराओ से numeri स्थिर index {
दिखाओ index, numeri[index]
}
}Over a range#
‥ builds a range. The lower bound is included, the upper bound is not.
faber format --locale en — English reader surfacemain {
for range 0‥3 const i {
print i
}
}faber format --locale la — canonical Faberincipit {
itera ab 0‥3 fixum i {
nota i
}
}faber format --locale th-TH — Thaiเริ่ม {
วน ช่วง 0‥3 คงที่ i {
บันทึก i
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
遍历 范围 0‥3 常量 i {
显示 i
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
遍歷 範圍 0‥3 定值 i {
註記 i
}
}faber format --locale vi — Vietnamesebắt_đầu {
lặp khoảng 0‥3 hằng i {
ghi_chú i
}
}faber format --locale ar — Arabicبداية {
كرر نطاق 0‥3 ثابت i {
اعرض i
}
}faber format --locale hi — Hindiआरंभ {
दोहराओ सीमा 0‥3 स्थिर i {
दिखाओ i
}
}per sets the step:
faber format --locale en — English reader surfacemain {
for range 0‥10 step 2 const i {
print i
}
}faber format --locale la — canonical Faberincipit {
itera ab 0‥10 per 2 fixum i {
nota i
}
}faber format --locale th-TH — Thaiเริ่ม {
วน ช่วง 0‥10 ต่อ 2 คงที่ i {
บันทึก i
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
遍历 范围 0‥10 步 2 常量 i {
显示 i
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
遍歷 範圍 0‥10 每 2 定值 i {
註記 i
}
}faber format --locale vi — Vietnamesebắt_đầu {
lặp khoảng 0‥10 qua 2 hằng i {
ghi_chú i
}
}faber format --locale ar — Arabicبداية {
كرر نطاق 0‥10 كل 2 ثابت i {
اعرض i
}
}faber format --locale hi — Hindiआरंभ {
दोहराओ सीमा 0‥10 प्रति 2 स्थिर i {
दिखाओ i
}
}While#
while loops while a condition holds.
faber format --locale en — English reader surfacemain {
var int i ← 0
while i ≺ 3 {
i ← i + 1
print i
}
}faber format --locale la — canonical Faberincipit {
varia numerus i ← 0
dum i ≺ 3 {
i ← i + 1
nota i
}
}faber format --locale th-TH — Thaiเริ่ม {
แปร จำนวน i ← 0
ขณะ i ≺ 3 {
i ← i + 1
บันทึก i
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
变量 整数 i ← 0
当 i ≺ 3 {
i ← i + 1
显示 i
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
變值 整數 i ← 0
當 i ≺ 3 {
i ← i + 1
註記 i
}
}faber format --locale vi — Vietnamesebắt_đầu {
biến số i ← 0
trong_khi i ≺ 3 {
i ← i + 1
ghi_chú i
}
}faber format --locale ar — Arabicبداية {
متغير عدد i ← 0
طالما i ≺ 3 {
i ← i + 1
اعرض i
}
}faber format --locale hi — Hindiआरंभ {
चर संख्या i ← 0
जबतक i ≺ 3 {
i ← i + 1
दिखाओ i
}
}Leaving early#
break breaks out; continue skips to the next iteration.
faber format --locale en — English reader surfacemain {
const list<int> numeri ← [1, 2, 3, 4, 5]
for from numeri const n {
if n ≡ 2 {
continue
}
if n ≻ 3 {
break
}
print n
}
}faber format --locale la — canonical Faberincipit {
fixum lista<numerus> numeri ← [1, 2, 3, 4, 5]
itera ex numeri fixum n {
si n ≡ 2 {
perge
}
si n ≻ 3 {
rumpe
}
nota n
}
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ รายการ<จำนวน> numeri ← [1, 2, 3, 4, 5]
วน ออก numeri คงที่ n {
ถ้า n ≡ 2 {
ไปต่อ
}
ถ้า n ≻ 3 {
หยุด
}
บันทึก n
}
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 列表<整数> numeri ← [1, 2, 3, 4, 5]
遍历 取自 numeri 常量 n {
如果 n ≡ 2 {
继续
}
如果 n ≻ 3 {
中断
}
显示 n
}
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 列表<整數> numeri ← [1, 2, 3, 4, 5]
遍歷 取自 numeri 定值 n {
若 n ≡ 2 {
繼續
}
若 n ≻ 3 {
中斷
}
註記 n
}
}faber format --locale vi — Vietnamesebắt_đầu {
hằng danh_sách<số> numeri ← [1, 2, 3, 4, 5]
lặp từ numeri hằng n {
nếu n ≡ 2 {
tiếp
}
nếu n ≻ 3 {
dừng
}
ghi_chú n
}
}faber format --locale ar — Arabicبداية {
ثابت قائمة<عدد> numeri ← [1, 2, 3, 4, 5]
كرر من numeri ثابت n {
إذا n ≡ 2 {
تابع
}
إذا n ≻ 3 {
اكسر
}
اعرض n
}
}faber format --locale hi — Hindiआरंभ {
स्थिर सूची<संख्या> numeri ← [1, 2, 3, 4, 5]
दोहराओ सेवन numeri स्थिर n {
यदि n ≡ 2 {
जारी
}
यदि n ≻ 3 {
तोड़ो
}
दिखाओ n
}
}A loop can also carry its own error handler — see
catch attaches to more than do.
Related: Control flow · Types and widths