for Loop
In this page:
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
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Count: {i}");
}
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: