311
The for loop in C# is a control structure that allows repeated execution of a block of code a fixed number of times.
It is commonly used when the number of iterations is known before the loop starts.
This tutorial covers:
- Syntax of the for Loop
- How the for Loop Works
- Example Usage of for Loop
- Using break and continue in a for Loop
- Nested for Loops
- The for Loop with Arrays and Lists
- Best Practices for Using for Loops
1. Syntax of the for Loop
The for loop consists of three parts:
- Initialization – Declares and initializes a loop variable.
- Condition – Checks if the loop should continue.
- Iteration Statement – Updates the loop variable after each iteration.
Syntax
for (initialization; condition; iteration) { // Code to be executed }
2. How the for Loop Works
- Initialization runs once before the loop starts.
- Condition is checked before each iteration.
- If true, the loop body executes.
- If false, the loop exits.
- Iteration Statement updates the loop variable.
- The process repeats until the condition becomes false.
Example: Basic for Loop
for (int i = 1; i <= 5; i++) { Console.WriteLine("Iteration " + i); }
Output:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5
- int i = 1; → Loop starts with i = 1.
- i <= 5; → Runs while i is less than or equal to 5.
- i++ → Increments i by 1 after each iteration.
3. Example Usage of for Loop
Counting Down Using a for Loop
for (int i = 5; i > 0; i--) { Console.WriteLine("Countdown: " + i); } Console.WriteLine("Liftoff!");
Output:
Countdown: 5 Countdown: 4 Countdown: 3 Countdown: 2 Countdown: 1 Liftoff!
- The loop starts from 5 and decrements until it reaches 1.
Skipping Numbers in a for Loop
for (int i = 0; i <= 10; i += 2) { Console.WriteLine(i); }
Output:
0 2 4 6 8 10
- The loop increments by 2 instead of 1.
4. Using break and continue in a for Loop
Using break to Exit the Loop Early
The break statement terminates the loop immediately when a condition is met.
for (int i = 1; i <= 10; i++) { if (i == 5) { Console.WriteLine("Stopping loop at " + i); break; // Exits the loop when i == 5 } Console.WriteLine(i); }
Output:
1 2 3 4 Stopping loop at 5
- The loop exits when i == 5.
Using continue to Skip an Iteration
The continue statement skips the current iteration and moves to the next.
for (int i = 1; i <= 5; i++) { if (i == 3) { Console.WriteLine("Skipping " + i); continue; // Skips iteration when i == 3 } Console.WriteLine(i); }
Output:
1 2 Skipping 3 4 5
- The loop skips i = 3 but continues running.
5. Nested for Loops
A for loop can be placed inside another for loop. This is useful for working with multi-dimensional data, such as matrices.
Example: Printing a Multiplication Table
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { Console.Write(i * j + "\t"); } Console.WriteLine(); }
Output:
1 2 3 2 4 6 3 6 9
- The outer loop controls the rows.
- The inner loop controls the columns.
6. The for Loop with Arrays and Lists
The for loop is commonly used to iterate through arrays and lists.
Example: Iterating Through an Array
int[] numbers = { 10, 20, 30, 40, 50 }; for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); }
Output:
10 20 30 40 50
- numbers.Length returns the size of the array.
- The loop runs from index 0 to Length – 1.
Example: Iterating Through a List
using System; using System.Collections.Generic; class Program { static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; for (int i = 0; i < names.Count; i++) { Console.WriteLine(names[i]); } } }
Output:
Alice Bob Charlie
- names.Count returns the number of elements in the list.
7. Best Practices for Using for Loops
Use a for Loop When the Number of Iterations is Known
for (int i = 0; i < 10; i++) { }
- for loops are best when the number of iterations is predefined.
Use Descriptive Variable Names
for (int counter = 0; counter < 5; counter++)
- Avoid using i, j, k unless necessary.
- Use meaningful names for readability.
Use break and continue Wisely
for (int i = 0; i < 10; i++) { if (i % 2 == 0) continue; // Skip even numbers }
- Use break to exit early and continue to skip iterations.
Avoid Infinite Loops
for (int i = 1; i > 0; i++) { } // Infinite loop
- Always ensure loop conditions will eventually become false.
Use foreach for Collections Instead of for
List<int> numbers = new List<int> { 1, 2, 3 }; foreach (int num in numbers) { Console.WriteLine(num); }
- foreach is simpler for iterating collections.
Conclusion
- The for loop is used when the number of iterations is known.
- It consists of an initialization, condition, and iteration statement.
- The break statement stops execution, while continue skips an iteration.
- Nested loops handle multi-dimensional data.
- Use for loops with arrays and lists when indexing is required.