Tracks
/
C#
C#
/
Syllabus
/
For Loops
Fo

For Loops in C#

19 exercises

About For Loops

A for loop allows one to repeatedly execute code in a loop until a condition is met.

for (int i = 0; i < 5; i++)
{
    System.Console.Write(i);
}

// => 01234

A for loop consists of four parts:

  1. The initializer: executed once before entering the loop. Usually used to define variables used within the loop.
  2. The condition: executed before each loop iteration. The loop continues to execute while this evaluates to true.
  3. The iterator: execute after each loop iteration. Usually used to modify (often: increment/decrement) the loop variable(s).
  4. The body: the code that gets executed each loop iteration.

for vs foreach loops

In general foreach-loops are preferrable over for loops for the following reasons:

  • A foreach loop is guaranteed to iterate over all values. With a for loop, it is easy to miss elements, for example due to an off-by-one error.
  • A foreach loop is more declarative, your code is communicating what you want it to do, instead of a for loop that communicates how you want to do it.
  • A foreach loop is foolproof, whereas with for loops it is easy to have an off-by-one error.
  • A foreach loop works on all collection types, including those that don't support using an indexer to access elements.

To guarantee that a foreach loop will iterate over all values, the compiler will not allow updating of a collection within a foreach loop:

char[] vowels = new [] { 'a', 'e', 'i', 'o', 'u' };

foreach (char vowel in vowels)
{
    // This would result in a compiler error
    // vowel = 'Y';
}

A for loop does have some advantages over a foreach loop:

  • You can start or stop at the index you want.
  • You can use any (boolean) termination condition you want.
  • You can skip elements by customizing the incrementing of the loop variable.
  • You can process collections from back to front by counting down.
  • You can use for loops in scenarios that don't involve collections.
Edit via GitHub The link opens in a new window or tab

Learn For Loops

Practicing is locked

Unlock 10 more exercises to practice For Loops