Loops
Repeat code with intay (while) and kuceli (for), and control iteration with jooji and soco.
Loops in Soplang
Soplang supports intay (while) and kuceli (for) loops. Use jooji to break out of a loop and soco to continue with its next iteration.
While Loop – intay
The loop body repeats while its condition is run:
while_basic.sop
abn i = 1 intay (i <= 5) { qor("i = " + i) i = i + 1 }
Use jooji and soco to control execution:
while_break_continue.sop
abn i = 0 intay (i <= 10) { i = i + 1 haddii (i % 2 == 0) { soco // Skip even numbers } haddii (i == 7) { jooji // Stop the loop when i = 7 } qor("i = " + i) // Prints 1, 3, then 5 }
For Loop – kuceli
The syntax is kuceli (variable start ilaa end :: step). The end bound is inclusive. The :: step part is optional and defaults to 1.
for_basic.sop
kuceli (i 1 ilaa 5) { qor("Tirada: " + i) }
Specify a step to change the increment:
for_step.sop
kuceli (j 2 ilaa 10 :: 2) { qor("Step: " + j) }
A negative step counts downward. A zero step produces an error.
kuceli (i 5 ilaa 1 :: -1) { qor(i) }
count_down.sop
door i = 5 intay (i >= 1) { qor(i) i = i - 1 }
ilaa is contextual rather than globally reserved: it has its loop meaning inside kuceli (...).
Loop Over a List
loop_over_list.sop
teed numbers = [10, 20, 30] kuceli (i 0 ilaa numbers.dherer() - 1) { qor("Element " + i + ": " + numbers[i]) }
Range with baaxad()
baaxad() creates a list of integers, similar to Python's range. Its stop bound is excluded, unlike the inclusive end bound of kuceli.
qor(baaxad(5)) // [0, 1, 2, 3, 4] qor(baaxad(2, 6)) // [2, 3, 4, 5] qor(baaxad(1, 10, 2)) // [1, 3, 5, 7, 9]
Nested Loops
nested_loops.sop
kuceli (i 1 ilaa 3) { intay (i > 0) { qor("Nested example") jooji } }
Factorial with While
factorial_while.sop
abn a = 5 abn result = 1 abn n = a intay (n > 0) { result = result * n n = n - 1 } qor(result)
Summary
| Keyword | Meaning |
|---|---|
intay | while loop |
kuceli | for loop |
jooji | break |
soco | continue |