Iteration Statements In C#

In this article, I will explain you about Iteration statements or Loop Controls in C#. There are many types of loop controls available in C# such as while loop, for loop, etc. I will try to demonstrate you all one by one with example.

Sometimes, it is required to repeat the task again and again; this is called looping or iteration. There are four types of looping in C# and these are Do, While, For and For each. They have their own benefits to use this in the program. You cannot replace the quality to another one. In the program, you perform some task or repeat some task on the basis of iteration. Iteration can be of different types. It might be one time checking and might be condition iteration. Looping does not end till the condition is false.

While Statement

It is a simple loop. It executes till the code block while the condition is true. If the condition is going to be false, the program will exit from the loop.



Example

  1. public class Program {  
  2.     public static void Main() {  
  3.         int num = 1;  
  4.         while (num <= 10) {  
  5.             Console.WriteLine("The number is {0}", num);  
  6.             num++;  
  7.         }  
  8.         Console.ReadLine();  
  9.     }  
  10. }  
Output



Do-While Statement

The do while loop is same as while loop except that it will be running at least one time if condition is matched or not. It is because it does not check the condition first time. So, it is guaranteed to execute the code of program at least one iteration.



Example 1
  1. public class Program {  
  2.     public static void Main() {  
  3.         int num = 1;  
  4.         do {  
  5.             Console.WriteLine("The number is {0}", num);  
  6.             num++;  
  7.         } while (num <= 10);  
  8.   
  9.         Console.ReadLine();  
  10.     }  
Output



Example 2
  1. public class Program {  
  2.     public static void Main() {  
  3.         int num = 1;  
  4.         do {  
  5.             Console.WriteLine("The number is {0}", num);  
  6.             num++;  
  7.         } while (num <= 0);  
  8.   
  9.         Console.ReadLine();  
  10.     }  
  11. }  
Output



IN the above example 2 we can see that if condition is false in while loop but the program still runs the first time in do while loop.

For Statement

The For loop is used if you know the start point and end point. You can run a statement or a block of statements repeatedly until a specified expression evaluates to false. It is useful where you know in advance how many times program should iterate.



Example
  1. public class Program {  
  2.     public static void Main() {  
  3.         for (int i = 1; i <= 5; i++) {  
  4.             Console.WriteLine("The iteration number is " + i);  
  5.         }  
  6.   
  7.         Console.ReadLine();  
  8.     }  
  9. }  
OutPut



In the above program, you can see the basic structure of for loop. Here int i=1; is a declaration of local variable which will participate throughout the loop.
i<=5; is the condition which decide how many times iteration will continue.

For each Statement

According to Microsoft,
 
"The foreach statement repeats a group of embedded statements for each element in an array or an object collection. It means if you are working on the array or collection of object than you should use foreach statement to iterate. So, foreach statement iterates the every element of collection and forwards it to next statement."



Example
  1. public class Program {  
  2.     public static void Main() {  
  3.         string[] daysOfWeek = new string[] {  
  4.             "Monday",  
  5.                 "Tuesday",  
  6.                 "Wednesday",  
  7.                 "Thursday",  
  8.                 "Friday"  
  9.         };  
  10.         foreach(string Day in daysOfWeek) {  
  11.             Console.WriteLine("The Day is : {0}", Day);  
  12.         }  
  13.   
  14.   
  15.         Console.ReadLine();  
  16.     }  
  17. }  
Output



Thanks for reading the article. Hope you enjoyed it.