Introduction
In C#, loop constructs allow you to repeatedly execute a block of code as long as a condition is true or for a specified number of times. Let’s go over each loop type in detail: for
, foreach
, while
, and do-while
.
1. for Loop
The for
loop is used when you know in advance how many times you want to execute a statement or a block of statements. It consists of three parts: an initializer, a condition, and an iterator.
Syntax
for (initializer; condition; iterator)
{
// Code to execute on each iteration
}
Parts For Loop
- Initializer: Sets the starting point, usually by declaring and initializing a loop counter variable.
- Condition: The loop runs as long as this condition is true.
- Iterator: Changes the loop counter after each iteration (e.g., incrementing or decrementing).
Example
for (int i = 0; i < 5; i++) { Console.WriteLine("Iteration: " + i); }
This loop will print “Iteration: 0” to “Iteration: 4”. Here’s how it works:
- Initializer:
int i = 0
setsi
to 0. - Condition:
i < 5
ensures the loop runs whilei
is less than 5. - Iterator:
i++
incrementsi
by 1 after each loop iteration.
This structure makes for
loops ideal for tasks where you need to execute code a specific number of times, like iterating over an array by index.
2. foreach Loop
The foreach
loop is designed to iterate over collections or arrays without needing to use an index or explicitly manage loop counters. It is best for when you want to process each element in a collection individually.
Syntax
foreach (var item in collection)
{
// Code to execute for each item
}
Example
int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine("Number: " + number); }
In this example, number
represents each element in the numbers
array, one by one. This loop will print each element from the array, “Number: 1” to “Number: 5”.
Key Points:
- You don’t need an index or iterator.
foreach
is only used for reading elements from the collection (you cannot modify the collection’s elements directly using aforeach
loop).
This makes foreach
an excellent choice for iterating over arrays, lists, or any other collection type.
3. while Loop
The while
loop is a control flow statement that allows you to repeat a block of code as long as a specified condition is true. The condition is checked before each iteration, so if the condition is false initially, the loop body will not execute at all.
Syntax
while (condition)
{
// Code to execute while condition is true
}
Example
int count = 0; while (count < 5) { Console.WriteLine("Count: " + count); count++; }
In this example, the loop will print “Count: 0” to “Count: 4”. Here’s how it works:
- Condition:
count < 5
must be true for the loop to run. - Body Execution: Inside the loop,
count++
incrementscount
after each iteration.
When count
reaches 5, the condition becomes false, and the loop terminates.
4. do-while Loop
The do-while
loop is similar to the while
loop, but there’s a key difference: in a do-while
loop, the condition is checked after the loop body executes. This means that the loop will always execute at least once, regardless of the condition.
Syntax
do
{
// Code to execute at least once
} while (condition);
Example
int count = 0; do { Console.WriteLine("Count: " + count); count++; } while (count < 5);
In this example, the loop will print “Count: 0” to “Count: 4”, similar to the while
loop. However, even if count
was initialized to a value greater than or equal to 5, the loop would still run once because the condition is evaluated after the loop body.