C# For Loop and Foreach Loop step by step

Introduction

Hello Friends, welcome to learn C# step by step with Gurunatha Dogi and Questpond. This is a place to understand c-sharp completely, Here we do provide self-learning materials like DVDs, Videos, E-books, Articles and so on. We do have paid learning scheme like one-to-one cost 20k+ (C# + ASP.NET + SQL Server) and one-to-group will cost 10k+ (C# + ASP.NET + SQL Server). Either you can avail self-learning material or training with faculty (Online training is also available via Microsoft live meeting and Skype) or learn from free videos and articles. Our objecttive to spread Information Technology knowledge at minimal cost.

For many more information feel free to contact us on a below mentioned details.

Warch our free C# Video i.e. learn csharp in 100 hours kick start with csharp programming.

Here in this session we will learn c# basics that is understand For Loop and ForEach Loop step by step using an example in a Visual Studio 2015.

What is use of Loops in C#

If there is a scenario where you need to execute any task for a specific times or "n" no of times then rather than writing that task to "n" times we can simply using c-sharp loops to execute that task to "n" number of times.

So to perform any repetitive task for "n" number of times then it is best practice to use Loops.

For Loops in C#

For loops are those loops which performs a specific task for "n" times, In this loop we can specify iteration value or n value. So use this loop if you know the iteration value.

for (; Boolean-expression;)

 ….Code goes here
}  
	class Program
    {
        static void Main(string[] args)
        {
            for(int k =0; k <= 10; k++)
            {
              Console.WriteLine("Print Number :  {0}",k);
        
            }
         }
     }

In our above example of code let's talk about of declaring for loop, First we have declared an integer variable "k" with a initial value "0" then we have declared a boolean expression "k <=100" means until this boolean value becomes false loop will continue its process, finally we have incremented an integer value.

Inside for loop we can specify nested loop or foreach loop or if condition or print any value like we did above code.

In our day-to-day programming developers uses for loop many times in their code. So it is very important to know for loop. If you any doubts or query feel free to talk to us.

ForEach Loops in C#

ForEach loop also works in a similar fashion as for loops work, that doing any specific repetitive task but the difference is that here we cannot specify "iteration value" here value is decided automatically depending on total collection of arrays or list generic or arraylist or object collections. Value will be picked automatically from these specified collections.

ForEach loop works only with these collection array values, So use this loop if you want to iterate / print list collection or array collection values. Depending on size of the array object it is going to perform its task.

foreach (  )
….code goes here
}   

 	class Program
    {
        static void Main(string[] args)
        {
            string[] Names = new string[] { "Shiv", "Khadak", "Athesham", "Ajay" };
            foreach (string name in Names){
              Console.WriteLine("Name of a Person is {0}",name);
            }  
        }
    }

Here in above example of ForEach we have executed or iterated string array values and printed them one by one. Like wise we can executed these task for array objects, list collections or any object collections using ForEach loop.

In a development world programmers use ForEach loop only if they want to iterate any object collection. This is too useful loop and widely used in c#

So hope you have understood use of For loop and ForEach loops now let's their differences.

Difference Between For and For Each Loop in C#

1 : For Loops executes a block of code until an expression returns false.

1 : ForEach loop executed a block of code through the items in object collections.

2 : For loop can execute with object collections or without any object collections.

2 : ForEach loop can execute with object collections only.

The for loop is a normal loop construct which can be used for multiple purposes where as foreach is designed to work only on Collections or IEnumerables object

Performance or speed wise for loop is better than ForEach loop.

Hope you have understood this simple article on Loops, if you have any doubts or questions feel free to ask us via below mentioned details.

+91-22-66752917
+91 9967590707
questpond@questpond.com / questpond@yahoo.com