LEARN VOID / 05

Control flow

Follow the logic.

Complete program for the synchronous entry adapter. The result below is the returned integer.

control-flow.voidDownload source
func entry(limit: i32) -> i32:
    var total = 0
    for number in [1, 3, 5, 7, 9]:
        if number > limit:
            break
        total += number
    return total
Input 5Expected result 9

Choose which values to visit

for number in [1, 3, 5, 7, 9]: visits five odd numbers in order. The list is deliberately short so the control flow stays visible.

Stop when the limit is reached

if number > limit: checks a Boolean condition; break exits the loop before adding an out-of-range number. Limit 5 adds 1 + 3 + 5, returning 9. This example only considers the five listed numbers, even if the limit is larger than nine.

Use one loop keyword

for remaining > 0: is the conditional form; its body must update the condition or exit. A bare for: repeats until an exit. continue skips to the next iteration. The collections guide demonstrates accumulating values from a growable list.