In this tutorial we will learn about various loops in C# programming language like for
loop, while
loop and do while
loop along with some useful loop control statements like break
statement and continue
statement.
Looping is a concept in any programming language to execute a set of statements multiple times or to iterate a given block of code multiple times. You will get a better understanding of it as we go through the practical implementation(code examples) of loops which are available in C# programming language.
-
for
loop
-
while
loop
-
do-while
loop
Let's see the implementation and use of each loop one by one.
1. for
loop
The for
loop is used to repeat a block of code/set of statements, several times. In C#, for
loop's syntax is same as in C and C++. Let's see the syntax,
for(counter initialization, condition, counter increment/decrement)
{
block of code (loops, nested loops, control statements, etc.)
}
Where along with the for
loop, we have to initialize a counter variable and initialize it with some value, then we have to provide a condition where we generally check the counter variable's value against a preset limit and the last part is the part where we either increment or decrement the counter variable.
Let's take an example where we will be using the for
loop to print numbers from 1 to 5.
Filename: Program.cs (example with a single for-loop)
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
for(int i=1; i<=5; i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}
Output:
1
2
3
4
5
In the above example, we are iterating a loop five times, each time we are executing the same statement but with an incremented value of the counter i
. Firstly, we have initialized the value of i
to 1. Secondly, we have given a condition that execute the loop till the value of i
is less than or equal to 5. Once the value of i
becomes greater than 5, come out of the loop to proceed further.
We can use a for
loop inside another for
loop. Using loops in this way is called as Nested Loops.
Filename: Program.cs (example with nested for loop)
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=0;j<=5;j++)
{
Console.WriteLine(i+" "+j);
// we will learn about break statement after loops
break;
}
}
Console.ReadKey();
}
}
}
Output:
1 0
2 0
3 0
4 0
5 0
In the above example, in every iteration, the nested for
loop is assigning the value as zero just because we have used break statement and we are not letting the inner loop run its course. The control will come out of the nested loop and go to the outer loop. Hence, every time in the nested inner loop the value zero gets assigned and printed. The loop will execute until the outer loop completes its iteration. In our case, it executes till i <= 5
.
2. while
loop
The while
loop is used when the number of iterations is not fixed. In such conditions, it is recommended to use the while
loop rather than the for
loop. Following is the syntax of the while
loop,
while(condition)
{
block of code (loops, nested loops, control statements, etc.)
}
Filename: Program.cs (example of while loop)
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
while(true)
{
Console.WriteLine("I am Learning!");
}
Console.ReadKey();
}
}
}
Output:
I am Learning!
I am Learning!
I am Learning!
In the above program, we have used while(true)
which means, it will execute the loop endless (infinite times) as the condition is always valid/true.
3. do-while loop
The do-while loop is used when the number of iterations are not fixed just like in case of a while
loop. The difference between while
and do-while is that do-while executes the body at least once because the condition is checked after the execution body. Hence, the code will always be executed once, after that the condition will be checked to see if the loop can be executed more times or not.
Folloing is the syntax of do-while loop,
do
{
//this block will always execute at least once
block of code (loops, nested loops, control statements, etc.)
} while(condition);
Let's take an example to understand it better,
Filename: Program.cs (example of do-while and while
loop)
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
int i=1;
do
{
Console.WriteLine("do-while loop executed "+i+" time(s)");
i++;
} while(i==0);
// using the while loop
while(i==0)
{
Console.WriteLine("while loop executed "+i+" time(s)");
}
Console.ReadKey();
}
}
}
Output:
do-while loop executed 1 time(s)
As you can see in the above example, it is clear that do-while loop executes at least one time whereas while
loop checks whether the condition is true or not and if the condition is found to be true, only then executes the block of code.
4. break
statement
The break
statement is used to come out from a block of code, be it a control statement(in case of switch
statements) or a for
loop. In the following example, we print the numbers from 1 to 5, but because of the break
statement, the loop prints the output as 1 only, as it will execute only once. Using the break
statement is just like forceful end of loop.
Filename: Program.cs
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
for(int i=1; i<=5; i++)
{
Console.WriteLine(i);
break;
}
Console.ReadKey();
}
}
}
Output:
1
5. continue
statement
The continue
statement is used to refresh the iteration or execution of a particular cycle of the loop. When a loop is executing, and there are 10 statements to be executed every time inside the loop, if we insert a continue
statement after the 5th statement, then in every iteration of the loop, after the execution of 5 statements, when the continue
statement is found, the loop skips the remaining statements and starts with the new iteration, i.e. again from the 1st statement.
In the following example, we are printing numbers from 1 to 5, but because of the continue
statement, the value 2 and the text Hello, World is not printed (skipped the code).
Filename: Program.cs
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
for(int i=1;i<=5;i++)
{
if(i==2)
{
continue;
Console.WriteLine("Hello, World!");
}
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}
Output:
1
3
4
5
In this tutorial, we learned about 3 different types of loops used in C# to execute code multiple times and we also learned about break
and continue
statements which are used for managing the flow control inside the loops.
You may also like: