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
- public class Program {
- public static void Main() {
- int num = 1;
- while (num <= 10) {
- Console.WriteLine("The number is {0}", num);
- num++;
- }
- Console.ReadLine();
- }
- }
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- public class Program {
- public static void Main() {
- int num = 1;
- do {
- Console.WriteLine("The number is {0}", num);
- num++;
- } while (num <= 10);
-
- Console.ReadLine();
- }
- }
Output Example 2- public class Program {
- public static void Main() {
- int num = 1;
- do {
- Console.WriteLine("The number is {0}", num);
- num++;
- } while (num <= 0);
-
- Console.ReadLine();
- }
- }
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- public class Program {
- public static void Main() {
- for (int i = 1; i <= 5; i++) {
- Console.WriteLine("The iteration number is " + i);
- }
-
- Console.ReadLine();
- }
- }
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- public class Program {
- public static void Main() {
- string[] daysOfWeek = new string[] {
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday"
- };
- foreach(string Day in daysOfWeek) {
- Console.WriteLine("The Day is : {0}", Day);
- }
-
-
- Console.ReadLine();
- }
- }
Output Thanks for reading the article. Hope you enjoyed it.