Kwizcom Forms

Loops in C#.Net

In this post, we will discuss Loops in C#.net.

Check my previous posts on Exception handling in C#.Net, Lock and Monitor statement in C# and Concurrent Collections in .NET 4.0.

SharePoint deveopment training course

If you are preparing for interviews, then this interview questions on Asp.Net, ADO.Net, WCF and Ajax will help you.

– Loops help to repeat a segment of code multiple times.

– C#.Net provides 3 types of Loops: for loop, for each loop and while or do…while loop.

for Loop:

– Through for loop you to repeat a block of code a set number of times, using a built-in counter. To create a for loop, you need to specify a starting value, an ending value, and the amount to increment with each pass.

syntax:
for (int i = 0; i < 10; i++)
{
// Code which will execute 10 times.
}

for each Loop:
– the foreach loop allows you to loop through the items in a set of data. The code will then loop until you’ve had a chance to process each piece of data in the set.

Example:
int[] intArray = {1,2,3};
foreach (int num in intArray)
{
num += 1;
}

while loop:
– A while loop tests a specific condition before or after each pass through the loop. When this condition evaluates to false, the loop is exited.

int i = 0;
while (i < 10)
{
i += 1;
}

Here the loop will execute till the value of i >=10. Here it will loop 10 times.

– The do..while syntax is also same. In case of do…while the condition will be tested at the end of each pass through the loop.

syntax:
int i = 0;
do
{
i += 1;
}
while (i < 10);

– The do…while loop will always execute the code at least once, because it doesn’t test the condition until the end.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →