Iteration Statements in C#
C# iteration statements are used to repeatedly execute a particular piece of code in a C# application. Consider a simple scenario where you have to print all the numbers from 1 to 100. If you print all the numbers individually, you will have to write 100 Console.WriteLine() statements. With loops, you can repeatedly execute a particular piece of code as many times with a few lines of code. In this article, you will workings of different types of loops or iteration statements supported by C#.
Table of Contents
For Loop
If you know the exact number of interactions for which you want to repeatedly execute a particular piece of code, you should use the for loop. The syntax of a for loop is as follows:
for (initial condition; test expression; update statement) { //code here }
A for loop has three main parts as shown in the above syntax. The initial condition is the condition at the beginning of the loop. The statement containing the initial condition is executed only once. The test expression is the statement that is executed every time before the execution of the code in the body of the loop. The update statement executes after every iteration of the code execution in the body of the loop.
Let’s see how you can print the odd numbers between 1 and 20 using a for loop:
using System; namespace DariaApp { class Program { static void Main(string[] args) { for (int i = 1; i < 21; i++) { if(i%2 ==1) { Console.WriteLine(i); } //code here } Console.ReadKey(); } } }
In the above script, in the initial condition the value of the integer i is set to 1. Next, in the text expression, you check if the value of integer i is less than 21. Since i is 1 during the first iteration, the text expression returns true and the body of the loop executes. After the first iteration, the value of i becomes 2 which is again less than 21, hence the loop executes again. The loop executes for 20 iterations. After 20 iterations, the value of the variable i becomes 21 and the test expression returns false. Hence, the loop terminates.
In the body of the loop, you simply check if the division of the value in the variable i by the integer 2 returns a remainder of 1, which means the value in the variable is odd. The value is printed on the console. Else, nothing happens and the next loop cycle executes. In the output below, you can see the odd integers between 1 and 20 inclusive.
Output:
1 3 5 7 9 11 13 15 17 19
While Loop
If you want your loop to execute indefinitely until a certain condition returns true, you can use a while loop. The syntax of a while loop is as follows. The while loop has simply one test expression. The loop keeps executing the text expression returns true.
while(test_expression) { // code }
Let’s now print all even numbers from 1 to 20.
using System; namespace DariaApp { class Program { static void Main(string[] args) { int i = 1; while(i < 21) { if (i%2 == 0) { Console.WriteLine(i); } i++; } Console.ReadKey(); } } }
From the above script, the while loop executes until the condition i < 21 returns true. You can see that i has been initialized to 1 outside the while loop. Finally, after the body of the while loop executes, the value of the variable i is incremented by 1. Inside the body, we check if the value in the variable i, when divided by 2 returns a remainder of 0. If the condition returns true, the value is printed on the console. Here is the output of the above script:
Output:
2 4 6 8 10 12 14 16 18 20
Foreach Loop
Foreach loop is an extended version of the for loop which iterates on a collection of items. The number of iterations of a foreach loop is equal to the number of items in a collection that the foreach loop iterates. The syntax of the foreach loop in C# is as follows:
foreach (var_type var_name in var_type_collection) { // code }
The following script shows an example of a foreach loop that iterates through an array of five integers and prints them on the console.
using System; namespace DariaApp { class Program { static void Main(string[] args) { int [] my_array = { 10, 20, 30, 40, 50 }; foreach(int i in my_array) { Console.WriteLine(i); } } } }
Output:
10 20 30 40 50
Other useful articles:
- TOP-10 Basic C# Interview Questions and Answers
- TOP-10 C# Questions for Advanced Programmers
- 10 Important C# Interview Questions and Answers
- Most Asked C# Interview Q & A
- C# Interview for Beginners
- TOP-10 Advanced C# Interview Questions and Answers
- Introduction to Functions in C#
- C# Jagged Arrays
- C Sharp Interview Questions for 5 Years Experience
- C Sharp Interview Questions for Testers
- C Sharp Questions for 10 Years Experience
- Iteration Statements in C#
- Exception Handling in C#
- C Sharp Methods
- C# Data Types
- C# Vs. C++
- Regular Expressions in C Sharp
- Defining Nested Methods in C Sharp
- C Sharp Dynamic Data Type
- C Custom Exceptions
- How to Generate Random Numbers with C Sharp
- Pass by Value vs Pass by Reference in C Sharp