Login
Signup as a Tutor

As a tutor you can connect with more than a million students and grow your network.

What Is The Difference In For...Loop And Foreach Loop?

N
Neelam S.
02 Aug 0 0

Loop is very important in C# Programing language. Loop is call iteration statement, loops are used for executing the repeated task.

 1. For...Loop:

The for…loop executes a statement or a block of statements repeatedly until a specified expression is false. For…loop contain the maximum and minimum bound value.

For…loop Declaration:

For (initialization, Condition, increment/decrement)

{

}

 Initialization like int i = 0; //minimum bound

Condition like i < 5; //maximum bound

Example of for…loop:

For(int i=0;i<=5;i++){

Console.WriteLine(i);

}

Example of for…loop with array:

Int[] a=new int[5]{10,20,30,40,50};

For(int i=0;i<5;i++)

{

Console.WriteLine(a[i]);

}

The for…loop always start with integer value. Means initialization of for…loop is done with int.

2. Foreach loop:

The foreach loop is a group of embedded statements for each element in an array or an object collection. There is no need to specify the maximum and minimum bound value. In foreach loop we can assign integer, string, float, char any type of data.

Example:

Foreach loop declaration:

Foreach(datatype variable_name in collection_name)

{

}

Example 2:

Int[] x=new int[5]{3,4,5,6,7};

Foreach(int k in x)

{

Console.WriteLine(k);

}

Example 3 foreach loop with string:

String[] fruits=new string[5]{“Apple”,”Banana”,”Kiwi”,”Mango”,”BlueBerry”};

Foreach(string k in fruits)

{

Console.WriteLine(k);

}

Example 4: Foreach loop with float data:

Float[] data=new float[3]{3400.56f,2000.53f,7800.23f};

Foreach(float k in data)

{

Console.writeLine(k);

}

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Object Initializers And Collection Initializers: A Syntactic Sugar In C# 3.0
i. Introduction: Object initializers and Collection initializers are part of C# 3.0.These two concepts add a flexibility, readability, and maintainability in C#. As we are C# developers, we should know...

Debendra Dash | 12 Oct

0 0
0
Coding Instructions To Prevent SQL-Injection
SQL-Injection is a very sensitive and very common technology to hack an application. I am describing a few techniques to make the SQL-Injection difficult. The main point of injection is a dynamic query....

Animesh Anand | 15 Jun

0 0
0
Extension method In C#
Introduction. Hello Guys, In this article, I will explain what is extension method and why and where we should use this. Let’s understand the term extension means we are going to create something...

Surya Pandey | 03 Jun

0 0
0
Understanding Delegate in C# .NET
C# Delegate technique Delegate is one of the fantastic tool & technique in C#.NET. Using deleagte technique the following benefits could be avaialed One can define a method without name (Anonymous...

P K Subudhi | 14 May

0 0
0
Trends in .Net Platform
.NET Framework is a software framework developed by Microsoft. It includes a large class library named Framework Class Library (FCL) and provides language interoperability (each language can use code written...

Cms | 19 Apr

0 0
0

Looking for C# .NET ?

Find best C# .NET in your locality on UrbanPro.

Do you offer C# .NET ?

Create Free Profile »
Sponsored

Find Best C# .NET ?

Find Now »