The main difference between for and foreach in C# is that for loop is used as a general purpose control structure while foreach loop is specifically used for arrays and collections.
Computer programming is the process of writing instructions to allow the computer to perform a task. C# is a programming language programmers use to write programs. Sometimes, it is necessary to repeat a set of statements again and again. Control structures help to achieve this task. For and foreach loops are two such control structures available in C# programming. Moreover, foreach loop is an enhanced for loop.
Key Areas Covered
1. What is for in C#
– Definition, Functionality
2. What is foreach in C#
– Definition, Functionality
3. Difference Between for and foreach in C#
– Comparison of key differences
Key Terms
Array, C#, for, foreach, Loop
What is for in C#
The for loop is available in C# to allow the programmer to iterate through a set of statements in a program. Its syntax is as follows.
for(initialization; test expression; update expression){
// code
}
The first step is initialization. Then the program checks the test expression. If it is true, the statements inside the for loop are executed. At the last statement of the loop, the program follows the update expression and then checks the test expression again. If it is true, the loop is executed again. At the end of the last statement, the program follows the update expression and then checks the test expression. And, this process repeats. At a point, when the test expression becomes false, the loop terminates and the control passes to the very next instruction after the for loop.
An example is as follows.
Initially, the number is 1; it is less than 10. Therefore, the code inside the for loop is executed. Console.WriteLine prints the number 1. Then the program increments the number by 1. Now the number is 2; it is less than 10. So, that number is printed to the screen. Then the program increments the number by 1. Now the number is 3. This process repeats. When the number is 9, it is less than 10, so it prints on the screen. Then the program increments the number by 1. Now the number is 10. The test expression is false. So, the for loop terminates.
What is foreach in C#
The foreach loop is used to retrieve elements of an array or a collection. It is an alternative to the for loop. This loop is capable of iterating through each item in an array or a collection. It helps to achieve the same task as the for loop but it is easier to read and write. It is also called the enhanced for loop.
The syntax is as follows.
for(data type item: collection){
//code inside the for each loop
}
Collection refers to the name of the array or the collection. The item refers to a single element in the collection. When the loop goes through the collection, each element is stored in the variable item. The statements inside the loop are executed untill the end of the collection.
An example is as follows.
The numbers is an array. It consists of 5 integer values. The foreach loop iterates through each element in the array. In every iteration, the elements are stored to ‘i’ variable and the code inside the loop is executed. Therefore, each number in the array is printed on the console.
Difference Between for and foreach in C#
Definition
The for loop is a control structure for specifying iteration that allows code to execute repeatedly while theforeach loop is a control structure for traversing items in an array or a collection. Thus, this is the main difference between for and foreach in C#.
Complexity
Moreover, for loop is difficult to read and write than the foreachloop. Hence, this is also a difference between for and foreach in C#.
Functionality
Functionality is the other main difference between for and foreach in C#. While for loop is a general purpose loo, for each loop is specially used for arrays and collections.
Conclusion
The for and foreach are two control flow structures available in C# programming. The programmer can execute a set of statements again and again using them. The difference between for and foreach in C# is that for loop is used as a general purpose control structure while foreach loop is specifically used for arrays and collections. In brief, both helps to execute code repeatedly but foreach loop is more specific to arrays and collections.
Reference:
1. “C# For Loop – Javatpoint.” Www.javatpoint.com, Available here.
2. C# File Handling – Dot Net Perls, Available here.
Leave a Reply