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
- C#
- Visual Basic
for (initializer; condition; increment)
{
// Code to execute
}
For counter = start To end [Step increment]
' Code to execute
Next
Example
- C#
- Visual Basic
for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Iteration {i}");
}
For i = 0 To 4
console.writeLine($"Iteration {i}")
Next
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
- C#
- Visual Basic
foreach (type item in collection)
{
// Code to execute for each item
}
For Each item As type In collection
' Code to execute for each item
Next
Example
- C#
- Visual Basic
string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Dim fruits As String() = {"Apple", "Banana", "Cherry"}
For Each fruit As String In fruits
console.writeLine(fruit)
Next
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.
- C#
- Visual Basic
while (condition)
{
// Code to execute
}
While condition
' Code to execute
End While
Example
- C#
- Visual Basic
int count = 0;
while (count < 3)
{
Console.WriteLine($"Count: {count}");
count++;
}
Dim count As Integer = 0
While count < 3
Console.writeLine($"Count: {count}")
count += 1
End While
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.
- C#
- Visual Basic
do
{
// Code to execute
} while (condition);
Do
' Code to execute
Loop While condition
Example
- C#
- Visual Basic
int count = 0;
do
{
Console.WriteLine($"Count: {count}");
count++;
} while (count < 3);
Dim count As Integer = 0
Do
console.writeLine($"Count: {count}")
count += 1
Loop While count < 3
Nested Loops
Loops can be nested within other loops. This is useful for multidimensional data structures like matrices.
Example
- C#
- Visual Basic
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine($"i: {i}, j: {j}");
}
}
For i As Integer = 0 To 2
For j As Integer = 0 To 2
console.writeLine($"i: {i}, j: {j}")
Next j
Next i
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
- C#
- Visual Basic
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
break; // Exit the loop when i is 3
}
Console.WriteLine($"Iteration {i}");
}
For i = 0 To 4
If i = 3 Then
Break
End If
console.writeLine($"Iteration {i}")
Next
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
- C#
- Visual Basic
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
continue; // Skip the iteration when i is 3
}
Console.WriteLine($"Iteration {i}");
}
For i = 0 To 4
If i = 3 Then
Continue For ' Skip the iteration when i is 3
End If
console.writeLine($"Iteration {i}")
Next
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.