Skip to main content

Looping Usage Guide

“Loop” is a core programming construct that is frequently used in OneStream Business Rule and Assembly file code development. They are used to execute a block of code repeatedly until a condition is met. This guide will discuss and compare the syntax and examples of the different types.

for loop

The for loop is used when the number of iterations is known. It consists of an initiator, a condition, and an increment/decrement component.

Syntax

for (initializer; condition; increment)
{
// Code to execute
}

Example

for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Iteration {i}");
}

foreach Loop

The foreach loop iterates over each element in a collection or array. It's particularly useful for iterating through items in a collection without needing to know the number of items.

Syntax

foreach (type item in collection)
{
// Code to execute for each item
}

Example

string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}

while Loop

A while loop continues executing a block of code until a certain condition is met (true). If the condition is not met up front, it will never enter the while loop. It's useful when the number of iterations is not known beforehand.

while (condition)
{
// Code to execute
}

Example

int count = 0;
while (count < 3)
{
Console.WriteLine($"Count: {count}");
count++;
}

do-while Loop

The do-while loop is similar to the while loop, but it guarantees that the loop body is executed at least once.

do
{
// Code to execute
} while (condition);

Example

int count = 0;
do
{
Console.WriteLine($"Count: {count}");
count++;
} while (count < 3);

Nested Loops

Loops can be nested within other loops. This is useful for multidimensional data structures like matrices.

Example

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine($"i: {i}, j: {j}");
}
}

Break and Continue

In C# and VB.NET, the Break and Continue statements are used to control the flow of loops.

Break Statement

The Break statement is used to exit a loop immediately. It is useful when you want to stop the loop based on a certain condition.

Example

for (int i = 0; i < 5; i++)
{
if (i == 3)
{
break; // Exit the loop when i is 3
}
Console.WriteLine($"Iteration {i}");
}

Continue Statement

The Continue statement is used to skip the current iteration of the loop and proceed to the next iteration. It is useful when you want to skip certain iterations based on a condition.

Example

for (int i = 0; i < 5; i++)
{
if (i == 3)
{
continue; // Skip the iteration when i is 3
}
Console.WriteLine($"Iteration {i}");
}

Best Practices

  • Avoid infinite loops. Always ensure that a termination condition exists.
  • Use foreach for collections
  • Minimize nesting. Too many nested loops can make the code hard to read, maintain, and especially troubleshoot if there is an error.
  • Use ‘break’ and ‘continue’ judiciously: Overuse can make the logic hard to follow.

Was this page helpful?