if (x > 10) {
Konsol:Print("big");
} else if (x > 5) {
Konsol:Print("medium");
} else {
Konsol:Print("small");
}
Var:Number i = 0;
while (i < 5) {
Konsol:Print(i);
i++;
}
for (Number i = 0; i < 10; i++) {
Konsol:Print(i);
}
The init clause can use Number declaration or a plain assignment. The increment supports ++, --, and = expr.
while (true) {
if (done == true) { break; }
}
Skips the rest of the current iteration and jumps to the next. In a for loop the increment still runs.
for (Number i = 0; i < 10; i++) {
if (i % 2 == 0) { continue; }
Konsol:Print(i); // prints odd numbers only
}
Evaluates an expression and executes the matching case block. Works with numbers and strings. Supports fall-through (omit break) and a default branch.
switch (expr) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}
break exits the switch. continue inside a switch propagates to the enclosing loop.
Var:String day = "Wed";
switch (day) {
case "Mon": Konsol:Print("Monday"); break;
case "Wed": Konsol:Print("Wednesday"); break;
default: Konsol:Print("another day");
}
Iterate over a List, Array, Map (yields keys, sorted), or String (yields characters) without a manual index counter.
foreach (varName in collectionName) {
// body — varName holds the current element / key
}
break and continue work the same as in for / while.
List:String fruits;
List:Push("apple", fruits);
List:Push("banana", fruits);
foreach (String item in fruits) {
Konsol:Print(item);
}
// String — one character per iteration
Var:String word = "hi";
foreach (ch in word) {
Konsol:Print(ch); // h i
}
// 03_control.ks — if/else if/else, while, for, break
Konsol:Print("=== Control Flow ===");
// if / else if / else
Var:Number score = 75;
Konsol:Print("score = ${score}");
if (score >= 90) {
Konsol:Print("grade: A");
} else if (score >= 75) {
Konsol:Print("grade: B");
} else if (score >= 60) {
Konsol:Print("grade: C");
} else {
Konsol:Print("grade: F");
}
// Nested if
Var:Boolean active = true;
Var:Number level = 5;
if (active == true) {
if (level > 3) {
Konsol:Print("active and high level");
} else {
Konsol:Print("active but low level");
}
}
// while loop
Konsol:Print("--- while ---");
Var:Number i = 1;
while (i <= 5) {
Konsol:Print("i = ${i}");
i++;
}
// while with break
Konsol:Print("--- while + break ---");
Var:Number n = 0;
while (true) {
n++;
if (n == 4) { break; }
}
Konsol:Print("broke at n = ${n}");
// for loop
Konsol:Print("--- for ---");
for (Var:Number j = 0; j < 5; j++) {
Konsol:Print("j = ${j}");
}
// for with compound increment
Konsol:Print("--- for step 2 ---");
for (Var:Number k = 0; k <= 10; k += 2) {
Konsol:Print("k = ${k}");
}
// for with break
Konsol:Print("--- for + break ---");
for (Var:Number x = 0; x < 100; x++) {
if (x == 3) { break; }
Konsol:Print("x = ${x}");
}
// Logical conditions
Konsol:Print("--- logical ---");
Var:Number a = 5;
Var:Number b = 10;
if (a > 0 && b > 0) {
Konsol:Print("both positive");
}
if (a > 100 || b > 5) {
Konsol:Print("at least one condition true");
}
// 21_continue.ks — continue statement in while and for loops
Konsol:Print("=== continue in while ===");
Var:Number i = 0;
while (i < 6) {
i++;
if (i == 3) { continue; } // skip printing 3
Konsol:Print(i);
}
// expected: 1 2 4 5 6
Konsol:Print("=== continue in for ===");
for (Var:Number j = 0; j < 8; j++) {
if (j % 2 == 0) { continue; } // skip even numbers
Konsol:Print(j);
}
// expected: 1 3 5 7
Konsol:Print("=== continue with accumulator ===");
Var:Number sum = 0;
for (Var:Number k = 1; k <= 10; k++) {
if (k == 5) { continue; } // skip 5
sum += k;
}
Konsol:Print(sum);
// expected: 50 (1+2+3+4+6+7+8+9+10)
// 22_switch.ks — switch / case / default statement
Konsol:Print("=== basic switch (number) ===");
Var:Number x = 2;
switch (x) {
case 1:
Konsol:Print("one");
break;
case 2:
Konsol:Print("two");
break;
case 3:
Konsol:Print("three");
break;
default:
Konsol:Print("other");
}
// expected: two
Konsol:Print("=== default branch ===");
Var:Number y = 99;
switch (y) {
case 1:
Konsol:Print("one");
break;
case 2:
Konsol:Print("two");
break;
default:
Konsol:Print("no match");
}
// expected: no match
Konsol:Print("=== switch on string ===");
Var:String day = "Wed";
switch (day) {
case "Mon":
Konsol:Print("Monday");
break;
case "Wed":
Konsol:Print("Wednesday");
break;
case "Fri":
Konsol:Print("Friday");
break;
default:
Konsol:Print("other day");
}
// expected: Wednesday
Konsol:Print("=== fall-through (no break) ===");
Var:Number n = 1;
switch (n) {
case 1:
Konsol:Print("fell into 1");
case 2:
Konsol:Print("fell into 2");
break;
case 3:
Konsol:Print("three");
break;
}
// expected: fell into 1
// fell into 2
Konsol:Print("=== switch inside loop ===");
for (Var:Number i = 0; i < 4; i++) {
switch (i) {
case 0:
Konsol:Print("zero");
break;
case 2:
Konsol:Print("two");
break;
default:
Konsol:Print("odd");
}
}
// expected: zero odd two odd
// 28_foreach.ks — foreach loop
// ── List ─────────────────────────────────────────────────────────────────────
Konsol:Print("=== List ===");
List:String fruits;
List:Push("apple", fruits);
List:Push("banana", fruits);
List:Push("cherry", fruits);
foreach (Var:String item in fruits) {
Konsol:Print(item); // apple banana cherry
}
// ── List with break ───────────────────────────────────────────────────────────
Konsol:Print("=== List break ===");
foreach (Var:String item in fruits) {
if (item == "banana") { break; }
Konsol:Print(item); // apple
}
// ── List with continue ────────────────────────────────────────────────────────
Konsol:Print("=== List continue ===");
foreach (Var:String item in fruits) {
if (item == "banana") { continue; }
Konsol:Print(item); // apple cherry
}
// ── Array ─────────────────────────────────────────────────────────────────────
Konsol:Print("=== Array ===");
Array:Number scores = {10, 20, 30};
foreach (Var:Number n in scores) {
Konsol:Print(n); // 10 20 30
}
// ── Map (keys, sorted) ────────────────────────────────────────────────────────
Konsol:Print("=== Map keys ===");
Map:New info;
Map:Set("city", "Manila", info);
Map:Set("country", "Philippines", info);
Map:Set("lang", "Filipino", info);
Var:String mapVal;
foreach (Var:String key in info) {
Map:Get(key, info, mapVal);
Konsol:Print(key + " = " + mapVal);
}
// city = Manila country = Philippines lang = Filipino (sorted by key)
// ── String (characters) ───────────────────────────────────────────────────────
Konsol:Print("=== String chars ===");
Var:String word = "hi";
foreach (Var:String ch in word) {
Konsol:Print(ch); // h i
}
// ── Nested foreach ────────────────────────────────────────────────────────────
Konsol:Print("=== Nested ===");
List:String rows;
List:Push("A", rows);
List:Push("B", rows);
List:String cols;
List:Push("1", cols);
List:Push("2", cols);
foreach (Var:String r in rows) {
foreach (Var:String c in cols) {
Konsol:Print(r + c); // A1 A2 B1 B2
}
}
// ── Accumulate with foreach ───────────────────────────────────────────────────
Konsol:Print("=== Sum ===");
List:Number nums;
List:Push(5, nums);
List:Push(10, nums);
List:Push(15, nums);
Var:Number total = 0;
foreach (Var:Number n in nums) {
total = total + n;
}
Konsol:Print(total); // 30
// ── 2D List ───────────────────────────────────────────────────────────────────
Konsol:Print("=== 2D List ===");
List:String row1;
List:Push("a", row1);
List:Push("b", row1);
List:String row2;
List:Push("c", row2);
List:Push("d", row2);
List:String grid;
List:Push("row1", grid);
List:Push("row2", grid);
foreach (List:String innerRow in grid) {
foreach (Var:String cell in innerRow) {
Konsol:Print(cell); // a b c d
}
}
// ── 2D Array ──────────────────────────────────────────────────────────────────
Konsol:Print("=== 2D Array ===");
Array:Number vec1 = {1, 2};
Array:Number vec2 = {3, 4};
Array:String matrix = {"vec1", "vec2"};
foreach (Array:Number innerVec in matrix) {
foreach (Var:Number x in innerVec) {
Konsol:Print(x); // 1 2 3 4
}
}
Konsol:Print("done");