Swazi For Loops (kwa)
SwaziLang provides two powerful types of for loops:
- Normal for loop (counting/step-based)
- For-each loop (iterating over arrays and objects)
Both use the kwa keyword ("for" in Swahili), but with different syntax and use cases.
1. Normal For Loop
This loop lets you repeat code a specific number of times, or while a condition is met.
Syntax:
kwa(init; condition; updater) {
// loop body
}INFO
Things to notice and be carefully with.
- You must wrap the parameters in parentheses
( ). - parameters are optional in the
kwaloop, but make sure you keep the two semicolons between them.kwa(;;){} initcan only be an assignment statement eg.a = 3do not usedata a = 3conditionruns after every iteration, if true the loop iterates if false it stops- it can be anything that resolves to true/false
- swazi supports two kinds of
updatercan only bei++/i--as increment/decrement. - Invalid updater patterns like
i+=2ori=i+1are not allowed if you want to use these you should omit the updater and use it inside the loop body as a statement. eg.
data i = 0; # init used outside here
kwa(;i < 10;) {
i+=2 # step by two
}kwa(x=0; x<10; x++):
chapisha xor with C-style braces:
kwa(x=0; x<10; x++) {
chapisha x
}- Parameters required in parentheses:
- Start value:
x=0 - Condition:
x<10 - Increment/update:
x++
- Start value:
Single-line shorthand (C-style only):
kwa(x=0; x<5; x++) { chapisha "Hello, " + x }Invalid patterns:
kwa x=0; x<10; x++ {} // ❌ Parameters not wrapped: Invalid!
kwa(x=0; x<10; x++): chapisha "Hello world" // ❌ Pythonic single-line: Invalid!Always use parentheses for parameters, and only use single-line shorthand with braces {}.
2. For-Each Loop (kwa kila)
When you want to loop through items in an array, or keys/values in an object, use kwa kila.
Array Iteration
data arr = [1, 2, 4, 10]
kwa kila value, index katika arr:
chapisha value + ", index: " + indexvalue: the current itemindex: optional; position in array (0, 1, 2...)
You can omit the index:
kwa kila value katika arr {
chapisha value
}Object Iteration
data obj = { a: 4, b: 17, c: 21 }
kwa kila key, value katika obj:
chapisha key + " => " + value- For objects:
key: property name (required)value: optional; property value
Note:
- Object keys are unordered; don't expect a specific order.
- Array iteration uses values (with optional index), object iteration uses keys (with optional value).
Important Note: Iterables in kwa kila Loops
Non-iterable values like numbers cannot be used directly:
swazikwa kila value katika 9: // ❌ Invalid! 9 is not iterableTo create a range for iteration, use the built-in function
Orodha(n):Orodha(n)creates an array ofnnull values.- Now you can safely iterate:
swazikwa kila val, i katika Orodha(100): // val is always null // i (optional) is the position/index (0 to 99) chapisha "Current index: " + iThis pattern is useful for running code a set number of times, or generating indices for further logic.
Loop Controls: simama & endelea
SwaziLang supports loop control keywords:
simama— "break" (stops the loop immediately)endelea— "continue" (skips to the next loop iteration)
Example with endelea (continue):
kwa(i=1; i<=10; i++):
kama i == 2:
endelea;
kama i % 2 sawa 1 :
chapisha i- When
i == 2, the loop skips the rest of the body and continues to nexti.
Example with simama (break):
kwa kila val, i katika Orodha(100):
kama i > 10:
simama;
chapisha i- The loop stops entirely when
i > 10.
Practical Patterns
Counting Loop
kwa(i=1; i<=5; i++):
chapisha "Count: " + iLooping Over an Array
data colors = ["red", "green", "blue"]
kwa kila color katika colors:
chapisha "Color: " + colorLooping Over Object Properties
data person = { name: "Amina", age: 22, city: "Dar" }
kwa kila key, value katika person:
chapisha key + ": " + valueCommon Mistakes
- Missing parentheses in normal kwa loop:swazi
kwa x=0; x<10; x++ {} // ❌ Invalid! - Pythonic single-line for is not allowed:swazi
kwa(x=0; x<5; x++): chapisha "Hello" // ❌ Invalid! - Confusing array/object kwa kila params:
- Array:
kwa kila value, index katika arr { ... } - Object:
kwa kila key, value katika obj { ... }
- Array:
- Trying to use a number as an iterable:swazi
kwa kila val katika 5: ... // ❌ Invalid!
Summary Table
| Loop Type | Syntax Example | Use Case |
|---|---|---|
| Normal kwa loop | kwa(x=0; x<10; x++) { ... } | Counting, custom steps |
| For-each kwa kila | kwa kila value katika arr { ... } | Loop through array values |
| For-each kwa kila | kwa kila key, value katika obj { ... } | Loop through object keys |
| Range loop | kwa kila val, i katika Orodha(n) { ... } | Loop n times (val is null) |
| Loop break/continue | simama, endelea | Control loop execution |
Practice Challenge
Write a loop to print all odd numbers from 1 to 10, skipping 2.
Solution
kwa(i=1; i<=10; i++):
kama i == 2:
endelea;
kama i % 2 sawa 1 :
chapisha iWrite a loop to print indices 0 to 10 using kwa kila and simama.
Solution
kwa kila val, i katika Orodha(100):
kama i > 10:
simama;
chapisha iRemember:
- Always wrap normal kwa loop parameters in parentheses.
- Use kwa kila for arrays (values/index) or objects (key/value).
- Use
Orodha(n)for range-style loops. - Use
simamaandendeleafor loop control. - Prefer one block style per loop for clarity.