Coding/Learn/csharp/for loops
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnThe Standard For Loop & Counting
11 / 20 0%
Lesson 11 of 20
5 mins read

The Standard For Loop & Counting

PreviousNext Lesson
In Plain English

When you know in advance how many times a code block needs to repeat, the for loop bundles initialization, condition checking, and stepping into a single compact header line.

The Mental Model

"A for loop is like a digital lap counter on a running track: (Start at Lap 1; Run until Lap 5; Add 1 lap each time you cross the finish line)."

Deep Dive: How It Works

Header Structure: for (initialization; condition; increment)

1. Initialization: Runs once before the loop begins (int i = 0;).

2. Condition: Checked before each cycle (i < 5;).

3. Increment: Executed after each cycle finishes (i++;).

Zero-Indexed Counting: Iterating N times usually starts at i = 0 and runs while i < N.

Syntax Blueprint

for (int i = 0; i < 5; i++) {
  Console.WriteLine($"Index: {i}");
}

Iterates 5 times with i taking values 0, 1, 2, 3, 4.

PointCore Rules to Remember

Point star
Compact Syntax: Keeps loop control logic together in one visible header line.
Point star
Zero-Based Indexing: Aligns perfectly with arrays and collections.
Point star
Custom Steps: Can step by any amount (e.g. i += 2) or decrement (i--).

Common Beginner Traps & How to Fix Them

Mistake:Using <= instead of < on zero-indexed loops: for (int i = 0; i <= 5; i++)

Why it happens: Runs 6 times (0, 1, 2, 3, 4, 5) instead of 5 times.

How to fix: Use i < count for exact counts.

Pro Tip: If iterating through an array, for (int i = 0; i < array.Length; i++) gives you both the index and the element!

Live Interactive Example

Hit Run Code to see it live
Accumulating Sum with For Loop
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: The for loop runs 5 times, adding each integer to total.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Print even numbers 2 through 8

  • PointWrite a for loop starting at int i = 2, running while i <= 8, stepping with i += 2.
  • PointPrint each number on a line.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates