Back to course

For Loops: Repeating Code

C# Zero to Hero: Comprehensive Programming Masterclass

The For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop.

Syntax

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

Components

  1. int i = 0: Initialization (starts the loop).
  2. i < 5: Condition (loop runs while this is true).
  3. i++: Increment (happens after each loop iteration).