Home » C# Tutorial » C# for Loop with Syntax and Examples

C# for Loop with Syntax and Examples

Like many other components of any programming language, loops and if-statements play an extremely important role in determining certain results of your program. They not only add a base to support the code but also make it easier for the developer by eliminating unnecessary code and simplifying the process. In today’s article, we will be taking a look at how we can use a C# for loop to do the same for us. 

We should mention that the usage and implementation of for loops in C# are very similar to that of for loops in C++, however, there are certain differences that we will be highlighting in this article. So, let us get right down to it.

What is a C# For Loop?

A for loop in C# is a special type of loop which allows us to keep track of what is known as an ‘iterating variable’. An iterating variable is nothing but a variable that will keep changing throughout the loop. It is like a situation wherein we have a variable, and generally, it is a variable that will keep track of how many times we have been through that specific loop, or it can even keep track of other parameters.

It is a special scenario in our programs, wherein we wish to have a loop, along with a variable that changes every time we go through the loop. In the following example, we will be demonstrating how this iterating variable will function:

using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)                   {
  int a = 1;
  while (a <= 3)
  {
    Console.WriteLine(a);
    a++;
  }
  Console.ReadLine();
}           
 
}

OUTPUT:

1
2
3

In the example above, a is the iterating variable that changes every single time we go through the loop in C#.

Essentially, this iterating variable helps us keep track of how many times the loop was executed in the example above. In this case, it was executed three times.  A C# for loop is created especially to implement these iterating variables.

With the help of a for loop in C#, we can achieve the same result as a while loop, as displayed in the example above, but with a much cleaner and simpler method.

How to use C# For Loop?

There are 3 main steps used in the process of a for loop in C#. In the example above, we saw how when we use a while loop, we need to declare the variable and output parameters outside the content of the loop itself, as the while loop could only contain the loop condition. While using C# for loops, however, we can do all three things within the body of the for loop. The three steps are as follows:

  1. Variable initialization: In this step, we declare the iterating variable that we will be using in our for loop.
  2. Loop condition: In this step, we define the condition that the variable needs to satisfy in the loop.
  3. Output Parameter: In this step, we specify how we want our variable to be displayed after it has been processed in the loop.

By using the example above, we can see how we can achieve the same result by using C# for loops, like so:

using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{ 
  for(int a = 1; a <= 3; a++)
  {
Console.WriteLine(a);
 
  }
  Console.ReadLine();
}           
 
}

OUTPUT:

1
2
3

C# For Loop Syntax:

for(variable initialization; loop condition; output parameter)
{
 
}

Now we can see how a for loop in C# is a continuous loop continuum that works to satisfy the given condition.

C# Examples:

Example 1:

Let us look at how we can access a database of student IDs with the help of a for loop in C#.

using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)                   {
 int[] StudentID = { 451, 452, 453, 454, 455, 456, 457, 458, 459, 460};
 for (int a = 0; a < StudentID.Length; a++)
 {
   Console.WriteLine(StudentID[a]);
 } 
  
  Console.ReadLine();
}
} 

OUTPUT:

451
452
453
453
455
456
457
458
459
460

Example 2:

Let us look at a few examples of how we can use for loops in C# in different circumstances. In the following example, we will look at how we can use a for loop to loop through all the elements of an array. 

using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)                   {
 int[] EvenNumbers = { 2, 4, 6, 8, 0};
 for (int a = 0; a < EvenNumbers.Length; a++)
 {
   Console.WriteLine(EvenNumbers[a]);
 } 
  
  Console.ReadLine();
}
}  

OUTPUT:

2
4
6
8
0

When to use While Loops or For Loops in C#?

If and when you come across an indefinite loop, you would like to use a while loops instead of a for loop, as the end of the loop is not specified. While loops are good for showing a value once, or potentially more. C# For loops are ideal for iteration through arrays, lists, or larger classes that have multiple elements. For loops work best when we know how many elements are there in the array or collection and the length of the same.

For loops make it extremely convenient for developers and programmers to iterate through large collections while only using a smaller line of code that makes the coding process easier and shorter. This also helps in shortening the processing duration.

For loops in C# are preferred by most developers and programmers due to the sheer convenience that it provides. It effectively shortens the coding time and effort put forth by the coder and simplifies the process of programming. For loops are easy to understand for professionals and beginners alike and can be used however many times you wish to within your C# code.

Do you know?
1. C# Enum
2. C# Dictionary
3. C# Switch Case
4. C# Substring
5. C# Foreach
6. C# Struct
7. C# Interface
8. C# for Loop

Pin It on Pinterest