Loops
Chuks provides four loop constructs for iterating over data. All loops in Chuks are expressions — they evaluate to a value (see Expressions for details).
While Loop
Section titled “While Loop”The simplest loop. Executes the body repeatedly while the condition is true.
var count = 0;while (count < 5) { println(count); count++;}// Output: 0, 1, 2, 3, 4Infinite Loop with Break
Section titled “Infinite Loop with Break”var i = 0;while (true) { if (i >= 10) { break; } i++;}println(i); // 10For Loop
Section titled “For Loop”C-style for loop with initialization, condition, and update expressions.
for (var i = 0; i < 10; i++) { println(i);}Increment and Decrement
Section titled “Increment and Decrement”// Counting up with ++for (var i = 0; i < 5; i++) { println(i); // 0, 1, 2, 3, 4}
// Counting down with --for (var i = 10; i > 0; i--) { println(i); // 10, 9, 8, ... 1}Compound Assignment
Section titled “Compound Assignment”// Step by 2for (var i = 0; i < 20; i += 2) { println(i); // 0, 2, 4, 6, ... 18}
// Step down by 3for (var i = 30; i > 0; i -= 3) { println(i); // 30, 27, 24, ... 3}Nested Loops
Section titled “Nested Loops”for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { println(string(i) + "," + string(j)); }}For…of Loop
Section titled “For…of Loop”Iterates over the values of an array or collection. This is the idiomatic way to loop over array elements.
var fruits = ["apple", "banana", "cherry"];for (var fruit of fruits) { println(fruit);}// Output:// apple// banana// cherryWith Typed Arrays
Section titled “With Typed Arrays”var numbers: []int = [10, 20, 30, 40, 50];var sum = 0;for (var n of numbers) { sum += n;}println(sum); // 150Iterating Over Strings
Section titled “Iterating Over Strings”var word = "Chuks";for (var ch of word) { println(ch);}// Output: C, h, u, k, sFor…in Loop
Section titled “For…in Loop”Iterates over the keys of a map. Use this to traverse map entries.
var scores: map[string]int = { "math": 90, "science": 85, "english": 92};
for (var subject in scores) { println(subject + ": " + string(scores[subject]));}Common Pattern: Key-Value Iteration
Section titled “Common Pattern: Key-Value Iteration”var config: map[string]string = { "host": "localhost", "port": "8080", "debug": "true"};
for (var key in config) { println(key + " = " + config[key]);}Break and Continue
Section titled “Break and Continue”Exits the loop immediately.
for (var i = 0; i < 100; i++) { if (i == 5) { break; } println(i);}// Output: 0, 1, 2, 3, 4Continue
Section titled “Continue”Skips the rest of the current iteration and moves to the next one.
for (var i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } println(i);}// Output: 1, 3, 5, 7, 9Break in While Loops
Section titled “Break in While Loops”var n = 1;while (true) { if (n > 100) { break; } n = n * 2;}println(n); // 128Continue in While Loops
Section titled “Continue in While Loops”var i = 0;var sum = 0;while (i < 20) { i++; if (i % 3 == 0) { continue; // Skip multiples of 3 } sum += i;}println(sum);Loops as Expressions
Section titled “Loops as Expressions”In Chuks, loops are expressions that evaluate to a value. The value of a loop is the result of the last expression evaluated in the body. See Expressions for more details.
// While as expressionvar i = 0;var result = while (i < 5) { i++; i * 10;};
// For as expressionvar total = 0;for (var i = 0; i < 5; i++) { total += i;}println(total); // 10Quick Reference
Section titled “Quick Reference”| Loop Type | Syntax | Use Case |
|---|---|---|
while | while (condition) { ... } | Repeat while condition is true |
for | for (init; cond; update) { ... } | Counted iteration |
for...of | for (var item of collection) { ... } | Iterate over array values |
for...in | for (var key in map) { ... } | Iterate over map keys |
| Control | Effect |
|---|---|
break | Exit the loop immediately |
continue | Skip to the next iteration |