Skip to content

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).

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, 4
var i = 0;
while (true) {
if (i >= 10) {
break;
}
i++;
}
println(i); // 10

C-style for loop with initialization, condition, and update expressions.

for (var i = 0; i < 10; i++) {
println(i);
}
// 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
}
// Step by 2
for (var i = 0; i < 20; i += 2) {
println(i); // 0, 2, 4, 6, ... 18
}
// Step down by 3
for (var i = 30; i > 0; i -= 3) {
println(i); // 30, 27, 24, ... 3
}
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
println(string(i) + "," + string(j));
}
}

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
// cherry
var numbers: []int = [10, 20, 30, 40, 50];
var sum = 0;
for (var n of numbers) {
sum += n;
}
println(sum); // 150
var word = "Chuks";
for (var ch of word) {
println(ch);
}
// Output: C, h, u, k, s

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]));
}
var config: map[string]string = {
"host": "localhost",
"port": "8080",
"debug": "true"
};
for (var key in config) {
println(key + " = " + config[key]);
}

Exits the loop immediately.

for (var i = 0; i < 100; i++) {
if (i == 5) {
break;
}
println(i);
}
// Output: 0, 1, 2, 3, 4

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, 9
var n = 1;
while (true) {
if (n > 100) {
break;
}
n = n * 2;
}
println(n); // 128
var i = 0;
var sum = 0;
while (i < 20) {
i++;
if (i % 3 == 0) {
continue; // Skip multiples of 3
}
sum += i;
}
println(sum);

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 expression
var i = 0;
var result = while (i < 5) {
i++;
i * 10;
};
// For as expression
var total = 0;
for (var i = 0; i < 5; i++) {
total += i;
}
println(total); // 10
Loop TypeSyntaxUse Case
whilewhile (condition) { ... }Repeat while condition is true
forfor (init; cond; update) { ... }Counted iteration
for...offor (var item of collection) { ... }Iterate over array values
for...infor (var key in map) { ... }Iterate over map keys
ControlEffect
breakExit the loop immediately
continueSkip to the next iteration