While Loops

Fig 1:


var i = 0;
while (i < 10) {
    print(i++);
}

Output:

[0.001s][warning][perf,memops] Cannot use file /tmp/hsperfdata_runner/6 because it is locked by another process (errno = 11)
0
1
2
3
4
5
6
7
8
9

Possible use of an uninitialized variable is a compile-time error:

Fig 4:


var s: string;
while (input("Should `s` be initialized? (Y/n)") == "Y") {
    s = input("Enter the value for `s`:");
}
print(s);

Compilation Errors:

while_EX4_example.claro:5: Variable <s> may not have been initialized!
print(s);
      ^
Warning! The following declared symbols are unused! [s]
2 Errors

Exiting a While Loop Early

You can exit a loop early by using the break keyword as below.

Fig 2:


var i = 0;
while (i < 10) {
    if (i == 5) {
      break;
    }
    print(i++);
}

Output:

0
1
2
3
4

Skipping to the Next Iteration of the While Loop

You can also skip ahead to the loop's next iteration by using the 'continue' keyword as below.

Fig 3:


var i = 0;
while (i < 10) {
    if (i % 2 == 0) {
      ++i;
      continue;
    }
    print(i++);
}

Output:

1
3
5
7
9