← Back to C# Course | Chapter 4: Control Flow | Lesson 3 of 7

for Loop

In this page:

  1. for Loop

for Loop

A for loop repeats a block a known number of times, controlled by an initializer, a condition, and an increment step. It's the standard choice when you know exactly how many iterations you need, such as walking through indices. All three parts of a for header are optional but conventionally used together.

Note: Use for when you need the loop index itself, not just each element.

Example: for Loop

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        for (int i = 1; i <= 5; i++)
        {
            Console.WriteLine($"Count: {i}");
        }
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.