10 minlesson

For Loops

For Loops

The for loop is perfect when you know how many times you want to iterate. It combines initialization, condition, and update in one statement.

Basic for Loop

csharp
1for (int i = 0; i < 5; i++)
2{
3 Console.WriteLine($"Iteration {i}");
4}

Output

1Iteration 0
2Iteration 1
3Iteration 2
4Iteration 3
5Iteration 4

Anatomy of a for Loop

csharp
1for (initialization; condition; update)
2{
3 // Loop body
4}
  1. Initialization: Runs once before the loop starts
  2. Condition: Checked before each iteration
  3. Update: Runs after each iteration

Execution Order

csharp
1for (int i = 0; i < 3; i++)
2{
3 Console.WriteLine(i);
4}
5
6// 1. int i = 0 (initialization)
7// 2. i < 3 → true (condition)
8// 3. Console.WriteLine(0) (body)
9// 4. i++ (update) → i = 1
10// 5. i < 3 → true (condition)
11// 6. Console.WriteLine(1) (body)
12// 7. i++ (update) → i = 2
13// 8. i < 3 → true (condition)
14// 9. Console.WriteLine(2) (body)
15// 10. i++ (update) → i = 3
16// 11. i < 3 → false (condition, loop ends)

Common Patterns

Counting from 1

csharp
1for (int i = 1; i <= 10; i++)
2{
3 Console.WriteLine(i); // 1, 2, 3, ... 10
4}

Counting Down

csharp
1for (int i = 10; i >= 1; i--)
2{
3 Console.WriteLine(i); // 10, 9, 8, ... 1
4}

Step by 2

csharp
1for (int i = 0; i <= 10; i += 2)
2{
3 Console.WriteLine(i); // 0, 2, 4, 6, 8, 10
4}

Custom Steps

csharp
1// Multiples of 5
2for (int i = 0; i <= 50; i += 5)
3{
4 Console.WriteLine(i); // 0, 5, 10, 15, ... 50
5}

Looping Through Characters

csharp
1// Print A to Z
2for (char c = 'A'; c <= 'Z'; c++)
3{
4 Console.Write($"{c} ");
5}
6// Output: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Variable Scope

The loop variable is only accessible inside the loop:

csharp
1for (int i = 0; i < 5; i++)
2{
3 Console.WriteLine(i); // OK
4}
5
6// Console.WriteLine(i); // Error: i doesn't exist here

To access the counter after the loop:

csharp
1int i;
2for (i = 0; i < 5; i++)
3{
4 Console.WriteLine(i);
5}
6Console.WriteLine($"Final value: {i}"); // OK: i = 5

Nested for Loops

Loops inside loops:

csharp
1for (int row = 1; row <= 3; row++)
2{
3 for (int col = 1; col <= 4; col++)
4 {
5 Console.Write($"({row},{col}) ");
6 }
7 Console.WriteLine(); // New line after each row
8}

Output

1(1,1) (1,2) (1,3) (1,4)
2(2,1) (2,2) (2,3) (2,4)
3(3,1) (3,2) (3,3) (3,4)

Pattern Printing

csharp
1// Right triangle
2int size = 5;
3for (int i = 1; i <= size; i++)
4{
5 for (int j = 1; j <= i; j++)
6 {
7 Console.Write("* ");
8 }
9 Console.WriteLine();
10}

Output

1*
2* *
3* * *
4* * * *
5* * * * *

Practical Examples

Calculate Factorial

csharp
1int n = 5;
2long factorial = 1;
3
4for (int i = 1; i <= n; i++)
5{
6 factorial *= i;
7}
8
9Console.WriteLine($"{n}! = {factorial}"); // 5! = 120

Sum of Even Numbers

csharp
1int sum = 0;
2
3for (int i = 2; i <= 100; i += 2)
4{
5 sum += i;
6}
7
8Console.WriteLine($"Sum of even numbers 2-100: {sum}"); // 2550

Print a Rectangle

csharp
1int width = 10;
2int height = 5;
3
4for (int row = 0; row < height; row++)
5{
6 for (int col = 0; col < width; col++)
7 {
8 if (row == 0 || row == height - 1 ||
9 col == 0 || col == width - 1)
10 {
11 Console.Write("*");
12 }
13 else
14 {
15 Console.Write(" ");
16 }
17 }
18 Console.WriteLine();
19}

Output

1**********
2* *
3* *
4* *
5**********

Advanced for Loop Features

Multiple Variables

csharp
1for (int i = 0, j = 10; i < j; i++, j--)
2{
3 Console.WriteLine($"i = {i}, j = {j}");
4}

Empty Sections

csharp
1int i = 0;
2for (; i < 5; ) // Initialization and update elsewhere
3{
4 Console.WriteLine(i);
5 i++;
6}
7
8// Infinite loop (be careful!)
9// for (;;) { }

for vs while

Both can do the same thing:

csharp
1// Using for
2for (int i = 0; i < 5; i++)
3{
4 Console.WriteLine(i);
5}
6
7// Using while (equivalent)
8int j = 0;
9while (j < 5)
10{
11 Console.WriteLine(j);
12 j++;
13}

When to Use Each

  • for: When you know the number of iterations
  • while: When iterations depend on a condition
  • do-while: When you need at least one iteration

Summary

In this lesson, you learned:

  • for loop syntax: for (init; condition; update)
  • Use for loops when you know the iteration count
  • Nest loops for multi-dimensional patterns
  • Loop variables are scoped to the loop
  • for loops can have multiple variables and custom steps

Next, we'll explore foreach loops for iterating over collections.