In this tutorial, we are going to learn For Loop in C#. W can use For Loop to execute a block of code repeatedly.

After completing this tutorial you will be able to understand:

  • What is For Loop?
  • Where to use For Loop?
  • How to use For Loop?

Introduction:

For Loop specifies that we can execute a particular block of code until the specified condition returns false.

The syntax for declaring For Loop.

for (variable Initialization; Condition; Increment/Decrement)
{

//code block that needs to execute inside for loop.

}

Where;

Variable Initialization: In this part of the loop we declare and initialize the variable which will be used in Condition and Increment/Decrement part. This part executed only once and for the only first time.

Condition: Condition returns the Boolean value. If it returns true the code block inside the for loop is executed and if it returns false the code block inside the for loop will not execute and the program flows transfer to next statement after the For Loop.

Increment/Decrement: This is the iteration Statement. It executes after each pass.

For Loop

Let’s Create an example of For Loop in C#.

Open Visual Studio and create a Console Application called ForLoopDemo.

For Loop

Write the following code in Program.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace ForLoopDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Printing Counting from 1 to 10.");
for (int iCount = 1; iCount <= 10; iCount++)
{
Console.WriteLine(iCount);



}
Console.ReadLine();
}
}
}

In the above program you can see we have declared and intialised the variable iCount by 1. This loop executes for 10 times by increamenting its value by 1.

Run the Program and look at the output screen.

For Loop

Nested For Loop:

A Loop inside a Loop is called the Nested Loop. Suppose I want to print table from 2 to 10. I can do this task using nested For Loop.

Again create a Console Application in C# called NestedFoorLoopDemo.

For Loop

Write the following code in Program.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace NestedFoorLoopDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Printing Table from 1 to 10");
for(int iCount=1;iCount<=10;iCount++)
{
Console.WriteLine("Table of {0}", iCount);
Console.WriteLine("\t");
for(int jCount=1;jCount<=10;jCount++)
{
Console.WriteLine("{0} X {1} = {2}", iCount, jCount, iCount * jCount);



}
Console.WriteLine("\t");
}
Console.ReadLine();
}
}
}

You can see in the above code we have declared a loop inside for loop.

Run the application and look over the output window.

For Loop

You can Download the complete Source Code for “NestedForLoopDemo” fromĀ here.

You can Download the complete Source Code for “ForLoopDemo” fromĀ here.

Conclusion:

We hope you understand this tutorial in C#. I will your appreciate Feedback, Comments and Suggestions.

View More:

Thank You.

Leave a Comment