Table of Contents

Looping is a technique used to execute code an arbitrary number of times.

The Loop Keyword

The loop keyword defines the most basic type of looping structure in Nada. This is most useful when processing each element in a container, where the container size may not be known.

var i = 0;
var sum = 0;
loop
{
  sum += i;
  Console.WriteLine(sum);
  if (i >= 3)
    return;
  ++i;
}
0
1
3

The values of sum printed are 0, 1, and 3 because sum += 0, sum += 1, and sum += 2 are all executed in sequence with a call to Console.WriteLine after each. Whatever code is in the block following loop will be repeatedly executed until the loop is escaped with return, break, or continue. return is a directive that leaves the function, and therefore can be used to exit a loop.

While and Do While

While the loop keyword is sufficient in executing code an arbitrary ammount of times, there are other types of loop blocks that make for cleaner code.

var i = 0;
var sum = 0;
while(i < 3)
{
  sum += i;
  Console.WriteLine(sum);
  ++i;
}
0
1
3

The above snippet will sum together numbers in sequence like in the Loop code snippet, but this time using the while keyword. while will run when i is 0, 1, and 2, and the sequence of sums will be the same as in the Loop code snippet.

var i = 0;
var sum = 0;
do
{
  sum += i;
  Console.WriteLine(sum);
  ++i;
}
while(i < 3)
0
1
3

The above snippet also produces the same result as the While code snippet, but with do while instead of while. The do while structure is similar to a while loop, but with the looping condition is checked after the loop is run. One should be mindful of the implications of do while, that whatever is in the code block following do will run at least once. Still, the do while block is executed when i is 0, 1, and 2.

var i = 100;
var sum = 0;
do
{
  sum += i;
  Console.WriteLine(sum);
  ++i;
}
while(i < 3)
100

Notice how the above code snippet is a modification of Do While 1, showing how choosing the wrong type of loop can result in logical errors. The initial value of i is now 100, and therefore the summation range is [100,3]. In the field of mathematics, [100,3] is an empty interval, and the proper sum would be 0. Since a do while is used here, the result is instead 100. If the above example instead used a while loop, the code block would never execute and the result would be 0.

For loop

A common use for looping is to iterate over a set, whether it's numbers or elements in a container. The for loop provides an even shorter syntax than the while loop.

var myArray = Array[Integer]();
var sum = 0;
for (var j = 0; j < 3; ++j)
{
  myArray.Add(j);
}
Console.WriteLine(myArray);

for(var i = 0; i < 3; ++i)
{
  sum += myArray.Get(i);
  Console.WriteLine(sum);
}
{0, 1, 2}
0
1
3

Notice how the above code snippet and the While code snippet loops produce the same result: 0, 1, and 3. First i is initialized to 0. Then in a loop, the i < 3 condition is checked, sum is increased and printed, and i is incremented at the end of the iteration. The for loop follows the format for(<initialization>; <condition>; <iteration>). In addition, the above code snippet uses an array. An array is a container with the indexable quality, which means the i-th element can be directly accessed. This access is done by calling the function myArray.Get.

For Each loop

The foreach loop is reserved for iterating over containers, and is shorter than its equivalent for loop.

var myArray = Array[Integer]();
var sum = 0;
for (var i = 0; i < 3; ++i)
{
  myArray.Add(i);
}
Console.WriteLine(myArray);

foreach (var val in myArray)
{
  sum += val;
  Console.WriteLine(sum);
}
{0, 1, 2}
0
1
3

Any array that is indexable is also //iteratable//. The Array container in Nada can be iterated over as shown in foreach (var val in myArray). Take note that the index isn't accessable in a foreach loop, and that foreach should only be used when the order that elements are iterated over isn't relevant.

Navigating Loops

There are two ways to exit from a loop. When the loop condition is evaluated to false, the loop will end. The other way is to use either return or break. As explained on the function page, return will exit the current function while break will leave the next highest loop. The following code snippet uses myArray from the For Each code snippet.

var target : Integer = 1;
for (var index : Integer = 0; index < myArray.Count; ++index)
{
  if(myArray.Get(index) == target)
  {
    Console.WriteLine(index);
    break;
  }
}
Console.WriteLine("Loop Finished");
1
Loop Finished

The above code snippet looks for the index of the value 1 in myArray. The array holds {0, 1, 2} from the For Each code snippet, so the index of the value 1 is 1. Once the break statement is reached, Nada will jump directly to the final }. This is the closing brace of the for loop block.

Nested Loops

Any loop inside another loop is considered nested. Be aware in these situations that break will exit out of only one loop, while return will leave all of them.

for (var i = 0; i < 3; ++i)
{
  for (var j = 0; j < 3; ++j)
  {
    if(j >= i)
      break;
    
    Console.WriteLine("(`i`, `j`)");
  }
}
(1, 0)
(2, 0)
(2, 1)

Notice how the break statement only exits the inner for loop. The string passed into Console.WriteLine will evaluate to (1, 0), (2, 0), and (2, 1). In other words, the result is printed when j < i.

Continue

The continue keyword operates similarly to break. While break will jump to the end of the loop's scope and exit the loop, continue will only jump to the end of the loop's scope.

for(var i = 0; i < 5; ++i)
{
  if(i == 2)
    continue;
  
  Console.WriteLine(i);
}
0
1
3
4

Notice how Console.WriteLine(i) executes for each i in the range [0, 4] with the exception of 2. When i == 2, continue is reached and the next iteration is reached. Also note that even when using continue in a for loop, the iterative statement ++i is still executed.

Condensed Loops

In the case that a loops only contains one statement within its scope (simliar to conditionals) then no {} brackets are needed.

for(var i = 0; i < 3; ++i)
  Console.WriteLine(i);
  Console.WriteLine("-");
0
1
2
-

Here you can see that the second instance of Console.WriteLine(i); is only executed once as it falls outside the scope of the for loop.

Related Material

Manual

Code Reference