Chaga Programming Language: Chapter 5: Iteration Statements

Iteration statements

  • The Chaga programming language supports four forms of loop iteration:
    • do-until
    • for
    • foreach
    • while
  • The do-until statement differs from for, foreach, and while by iterating at least once.

DO-UNTIL statement

Usage:

  • do {statement block} until (expression)

Example 1: do-until statement

declare int i;
set i = 10;
do
{
  print (i, "\n");
  set i = i - 1;
}
until (i == 5);

Output from example 1

10
9
8
7
6

FOR statement

Usage:

  • for (identifier = integer or real constant to integer or real constant) {statement block}
  • for (identifier = integer or real constant to integer or real constant step integer or real constant) {statement block}

Example 2: for statement

declare int i;
for (i = 0 to 10 step 2)
{
  print (i);
  if (i < 10)
  {
    print (" ");
  }
}
print ("\n");

Output from example 2: for statement

0 2 4 6 8 10

FOREACH statement

Usage:

  • foreach (identifier) {statement block}
  • foreach (list) {statement block}

Example 3: foreach statement

declare list my_list;
set my_list = {1, 2, 3};
foreach element (my_list)
{
  print (element, "\n");
}
print ("\n");

Output from example 3: foreach statement

1
2
3

Example 4: foreach statement

foreach element ({1, 2, 3, 5, 7, 11, 13, 17, 19})
{
  print (element, "\n");
}
print ("\n");

Output from example 4: foreach statement

1
2
3
5
7
11
13
17
19

Example 5: foreach statement

declare list my_list;
set my_list = {1, {2, 3}, {4, 5, 6}, {7, {8, {9, 10}}}, 11, 12};
foreach element (my_list)
{
  print (element, "\n");
}

Output from example 5: foreach statement

1
{2, 3}
{4, 5, 6}
{7, {8, {9, 10}}}
11
12

WHILE statement

Usage:

  • while (expression) {statement block}

Example 6: while statement

declare int i;
set i = 10;
while (i >= 5)
{
  print (i, "\n");
  set i = i - 1;
}

Output from example 6: while statement

10
9
8
7
6
5


Copyright © 2025, 2026 Robert James Bruce.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is available at https://www.gnu.org/licenses/fdl-1.3.html