C# for loop


The for loop in C# is a control flow statement used to repeatedly execute a block of code a specific number of times. It is particularly useful when you know in advance how many times the loop should run.

Syntax of for Loop:

for (initialization; condition; increment/decrement) { // Code to be executed }

Explanation:

  • Initialization: This section is executed once at the beginning of the loop. It usually initializes a loop control variable (e.g., int i = 0).
  • Condition: This condition is checked before each iteration. If it's true, the loop continues; if false, the loop terminates.
  • Increment/Decrement: This section is executed at the end of each iteration to update the loop control variable (e.g., i++ to increment the variable by 1).
  • Loop Body: The block of code that will execute repeatedly while the condition is true.

Example of a Basic for Loop:

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

Explanation:

  • Initialization: int i = 1 initializes the loop control variable i with a starting value of 1.
  • Condition: i <= 5 ensures the loop runs as long as i is less than or equal to 5.
  • Increment: i++ increases the value of i by 1 after each iteration.
  • Loop Body: Inside the loop, the current value of i is printed.

Output:

Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5

How the Loop Works:

  1. First Iteration: i = 1, condition 1 <= 5 is true, so "Iteration: 1" is printed. Then i++ makes i = 2.
  2. Second Iteration: i = 2, condition 2 <= 5 is true, so "Iteration: 2" is printed. Then i++ makes i = 3.
  3. This continues until i = 6, at which point the condition 6 <= 5 becomes false, and the loop terminates.

Another Example with a Decrementing for Loop:

for (int i = 5; i >= 1; i--) { Console.WriteLine("Countdown: " + i); }

Explanation:

  • Initialization: int i = 5 starts the loop with i initialized to 5.
  • Condition: i >= 1 ensures the loop continues as long as i is greater than or equal to 1.
  • Decrement: i-- decreases the value of i by 1 after each iteration.
  • Loop Body: Prints the current value of i during each iteration.

Output:

Countdown: 5 Countdown: 4 Countdown: 3 Countdown: 2 Countdown: 1

Using the break Statement in a for Loop:

The break statement can be used to exit the loop early, based on a condition.

for (int i = 1; i <= 5; i++) { if (i == 3) { break; // Exit the loop when i equals 3 } Console.WriteLine("Iteration: " + i); }

Output:

Iteration: 1 Iteration: 2

In this example, the loop exits prematurely when i == 3, so it prints only the first two iterations.


Using the continue Statement in a for Loop:

The continue statement skips the current iteration and jumps to the next one.

for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip the iteration when i equals 3 } Console.WriteLine("Iteration: " + i); }

Output:

Iteration: 1 Iteration: 2 Iteration: 4 Iteration: 5

Here, the loop skips printing when i == 3 and continues with the next iteration.


Using a for Loop to Iterate Through an Array:

string[] colors = { "Red", "Green", "Blue", "Yellow" }; for (int i = 0; i < colors.Length; i++) { Console.WriteLine("Color: " + colors[i]); }

Explanation:

  • Initialization: int i = 0 starts with the first element of the array.
  • Condition: i < colors.Length ensures the loop runs until the last element in the array.
  • Increment: i++ increments the index after each iteration.
  • Loop Body: Prints each color from the array using colors[i].

Output:

Color: Red Color: Green Color: Blue Color: Yellow

Summary of the for Loop in C#:

  • The for loop is ideal for iterating a specific number of times.
  • The three main components are initialization, condition, and increment/decrement.
  • You can control the loop flow using break to exit early or continue to skip an iteration.
  • The for loop is often used with arrays or collections to iterate over their elements by index.