Renderingen-US

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.

ModeWalks
fromthe values of a collection
refthe keys of a table, or the indices of a list
rangea numeric range

Over values#

reader locale
faber convert --to en — English reader surface
main {
    const list<int> numeri ← [1, 2, 3]
    for from numeri const n {
        print n
    }
}
faber convert --to la — canonical Faber
incipit {
    fixum lista<numerus> numeri ← [1, 2, 3]
    itera ex numeri fixum n {
        nota n
    }
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ รายการ<จำนวน> numeri ← [1, 2, 3]
    วน ออก numeri คงที่ n {
        บันทึก n
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 列表<整数> numeri ← [1, 2, 3]
    遍历 取自 numeri 常量 n {
        显示 n
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 列表<整數> numeri ← [1, 2, 3]
    遍歷 取自 numeri 定值 n {
        註記 n
    }
}
faber convert --to vi — Vietnamese
bắt_đầu {
    hằng danh_sách<số> numeri ← [1, 2, 3]
    lặp từ numeri hằng n {
        ghi_chú n
    }
}
faber convert --to ar — Arabic
بداية {
    ثابت قائمة<عدد> numeri ← [1, 2, 3]
    كرر من numeri ثابت n {
        اعرض n
    }
}
faber convert --to hi — Hindi
आरंभ {
    स्थिर सूची<संख्या> numeri ← [1, 2, 3]
    दोहराओ सेवन numeri स्थिर n {
        दिखाओ n
    }
}

In lockstep#

from also accepts a comma series: give it N collections and N name binders, and the loop walks them all in lockstep — one shared index, one value from each list per turn:

main {
    const list<string> nomina ← ["Alia", "Brunus"]
    const list<int> aetates ← [34, 27]
    for from nomina, aetates at [i] const name, aetas {
        print i, name, aetas
    }
}

at (English at) names the shared index. The sources are listas, the binders are plain names, and the first source owns the length: if a later list is shorter, the loop stops with a runtime error rather than silently truncating to the minimum.

Over keys#

reader locale
faber convert --to en — English reader surface
main {
    const map<string, string> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    for ref persona const clavis {
        print clavis, persona[clavis]
    }
}
faber convert --to la — canonical Faber
incipit {
    fixum tabula<textus, textus> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    itera de persona fixum clavis {
        nota clavis, persona[clavis]
    }
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ ตาราง<ข้อความ, ข้อความ> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    วน จาก persona คงที่ clavis {
        บันทึก clavis, persona[clavis]
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 映射<文本, 文本> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    遍历 借自 persona 常量 clavis {
        显示 clavis, persona[clavis]
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 表格<文字, 文字> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    遍歷 從 persona 定值 clavis {
        註記 clavis, persona[clavis]
    }
}
faber convert --to vi — Vietnamese
bắ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 convert --to ar — Arabic
بداية {
    ثابت جدول<نص, نص> persona ← { "nomen": "Marcus", "urbs": "Roma" }
    كرر عن persona ثابت clavis {
        اعرض clavis, persona[clavis]
    }
}
faber convert --to 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:

reader locale
faber convert --to en — English reader surface
main {
    const list<int> numeri ← [10, 20, 30]
    for ref numeri const index {
        print index, numeri[index]
    }
}
faber convert --to la — canonical Faber
incipit {
    fixum lista<numerus> numeri ← [10, 20, 30]
    itera de numeri fixum index {
        nota index, numeri[index]
    }
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ รายการ<จำนวน> numeri ← [10, 20, 30]
    วน จาก numeri คงที่ index {
        บันทึก index, numeri[index]
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 列表<整数> numeri ← [10, 20, 30]
    遍历 借自 numeri 常量 index {
        显示 index, numeri[index]
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 列表<整數> numeri ← [10, 20, 30]
    遍歷 從 numeri 定值 index {
        註記 index, numeri[index]
    }
}
faber convert --to vi — Vietnamese
bắ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 convert --to ar — Arabic
بداية {
    ثابت قائمة<عدد> numeri ← [10, 20, 30]
    كرر عن numeri ثابت index {
        اعرض index, numeri[index]
    }
}
faber convert --to 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.

reader locale
faber convert --to en — English reader surface
main {
    for range 0‥3 const i {
        print i
    }
}
faber convert --to la — canonical Faber
incipit {
    itera ab 0‥3 fixum i {
        nota i
    }
}
faber convert --to th-TH — Thai
เริ่ม {
    วน ช่วง 0‥3 คงที่ i {
        บันทึก i
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    遍历 范围 0‥3 常量 i {
        显示 i
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    遍歷 範圍 0‥3 定值 i {
        註記 i
    }
}
faber convert --to vi — Vietnamese
bắt_đầu {
    lặp khoảng 0‥3 hằng i {
        ghi_chú i
    }
}
faber convert --to ar — Arabic
بداية {
    كرر نطاق 0‥3 ثابت i {
        اعرض i
    }
}
faber convert --to hi — Hindi
आरंभ {
    दोहराओ सीमा 0‥3 स्थिर i {
        दिखाओ i
    }
}

step sets the step:

reader locale
faber convert --to en — English reader surface
main {
    for range 0‥10 step 2 const i {
        print i
    }
}
faber convert --to la — canonical Faber
incipit {
    itera ab 0‥10 per 2 fixum i {
        nota i
    }
}
faber convert --to th-TH — Thai
เริ่ม {
    วน ช่วง 0‥10 ต่อ 2 คงที่ i {
        บันทึก i
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    遍历 范围 0‥10 步 2 常量 i {
        显示 i
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    遍歷 範圍 0‥10 每 2 定值 i {
        註記 i
    }
}
faber convert --to vi — Vietnamese
bắt_đầu {
    lặp khoảng 0‥10 qua 2 hằng i {
        ghi_chú i
    }
}
faber convert --to ar — Arabic
بداية {
    كرر نطاق 0‥10 كل 2 ثابت i {
        اعرض i
    }
}
faber convert --to hi — Hindi
आरंभ {
    दोहराओ सीमा 0‥10 प्रति 2 स्थिर i {
        दिखाओ i
    }
}

While#

while loops while a condition holds.

reader locale
faber convert --to en — English reader surface
main {
    var int i ← 0
    while i ≺ 3 {
        i ← i + 1
        print i
    }
}
faber convert --to la — canonical Faber
incipit {
    varia numerus i ← 0
    dum i ≺ 3 {
        i ← i + 1
        nota i
    }
}
faber convert --to th-TH — Thai
เริ่ม {
    แปร จำนวน i ← 0
    ขณะ i ≺ 3 {
        i ← i + 1
        บันทึก i
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    变量 整数 i ← 0
    当 i ≺ 3 {
        i ← i + 1
        显示 i
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    變值 整數 i ← 0
    當 i ≺ 3 {
        i ← i + 1
        註記 i
    }
}
faber convert --to vi — Vietnamese
bắt_đầu {
    biến số i ← 0
    trong_khi i ≺ 3 {
        i ← i + 1
        ghi_chú i
    }
}
faber convert --to ar — Arabic
بداية {
    متغير عدد i ← 0
    طالما i ≺ 3 {
        i ← i + 1
        اعرض i
    }
}
faber convert --to hi — Hindi
आरंभ {
    चर संख्या i ← 0
    जबतक i ≺ 3 {
        i ← i + 1
        दिखाओ i
    }
}

Leaving early#

break breaks out; continue skips to the next iteration.

reader locale
faber convert --to en — English reader surface
main {
    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 convert --to la — canonical Faber
incipit {
    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 convert --to th-TH — Thai
เริ่ม {
    คงที่ รายการ<จำนวน> numeri ← [1, 2, 3, 4, 5]
    วน ออก numeri คงที่ n {
        ถ้า n ≡ 2 {
            ไปต่อ
        }
        ถ้า n ≻ 3 {
            หยุด
        }
        บันทึก n
    }
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 列表<整数> numeri ← [1, 2, 3, 4, 5]
    遍历 取自 numeri 常量 n {
        如果 n ≡ 2 {
            继续
        }
        如果 n ≻ 3 {
            中断
        }
        显示 n
    }
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 列表<整數> numeri ← [1, 2, 3, 4, 5]
    遍歷 取自 numeri 定值 n {
        若 n ≡ 2 {
            繼續
        }
        若 n ≻ 3 {
            中斷
        }
        註記 n
    }
}
faber convert --to vi — Vietnamese
bắ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 convert --to ar — Arabic
بداية {
    ثابت قائمة<عدد> numeri ← [1, 2, 3, 4, 5]
    كرر من numeri ثابت n {
        إذا n ≡ 2 {
            تابع
        }
        إذا n ≻ 3 {
            اكسر
        }
        اعرض n
    }
}
faber convert --to 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