C# Loop Control

In CSharp, loop statement allow you to execute a statement multiple times. CSharp has different veriety of loop statement such as for loop, while loop, do while loop, for each loop which will used based on programming requirment. Loop also usefull when you need to iterate arrays.

For Loop

For Loop consist of three variables initialization, condition and operation. Where initialization is used to set the initial value of the loop, condition is a boolean expression which decide whether the loop will continue and the operation to perform on the counter variable value.

Syntax

for(initialization;condition;operation) statement;

Example1

using System;
class ForLoopDemo
{
	static void main()
	{
		for(int i = 0;i < 10;i++) 
			Console.WriteLine(i); // single statement;
		
		for(int i = 0;i < 5;i++) 
		{
			if(i > 3)             //multiple statement
				Console.WriteLine(i);  
			else
				Console.WriteLine(i);
		}
	}
}

This will produce following result

0
1
2
3
4
5

Using break to exit from loop

Example2

using System;
class ForLoopWithBreakDemo
{
	static void main()
	{	
		for(int i = 0;i < 10;i++) 
		{
			Console.WriteLine(i); 
			if(i > 5)           
				break;   //breaking the loop
		}
	}
}

This will produce following result

0
1
2
3
4
5

Loop with no body

Example3

using System;
class ForLoopNoBodyDemo
{
	static void main()
	{	
	int sum = 0;
		for(int i = 0;i < 10; sum =+ i++);
		Console.WriteLine(sum); 
	}
}

This will produce following result

10

For Each Loop

For Each Loop is used to loop through the each element of array or collection. This is basically used to read all the elements in array or collections. The for each loop contains type variable and collection or array. Whereas type variable must be the same as type in the collection elements and collection could be any array or custome type array. Here type could be use as var also, in this case compiler will determine type at run time.

The improtant thing here is foreach loop through an array sequentially from start to end. It is similar to For loop except we don't have to define an incrementer and increment it and there is also no condition to be evaluated

Syntax

foreach(type variable in collection) statement;

For Each Loop with array

Example1

using System;
class ForEachLoopDemo
{
	static void main()
	{	
		int[] sum = new int[5];
		foreach(int i in sum)
			Console.WriteLine(i); 
	}
}

This will produce following result

0
1
2
3
4
5

For Each Loop with array list

Example2

using System;
class ForEachLoopDemo
{
	static void main()
	{	
		ArrayList list = new ArrayList();
		list.Add("One");
		list.Add("Two");
		list.Add("Three");
		list.Add("Four");
		foreach(string str in list)
			Console.WriteLine(str); 
	}
}

This will produce following result

One
Two
Three
Four

Using break to exit from For Each Loop

Example3

using System;
class ForEachLoopWithBreakDemo
{
	static void main()
	{	
		ArrayList list = new ArrayList(); // using arraylist as input collection
		list.Add("One");
		list.Add("Two");
		list.Add("Three");
		list.Add("Four");
		foreach(string str in list)
		{		
			Console.WriteLine(str); 
			if(str == "Three")     	// meet search condition
				break;				// breaking the loop here
		}
	}
}

This will produce following result

One
Two
Three

While Loop

In CSharp while loop executes a block of codes as long as a given condition or test expression is a true. In this loop, we define the incrementer outside the loop and then increment it inside the loop in a separate statement. If the given condition or test expression is a true then code block will be executed.

After the code is executed, the given condition or test expression will again be evaluated and the loop will continue till the given condition or the test expression is found to be true.

Syntax

while(condition) statement;

Example

using System;
class WhileLoopDemo
{
	static void main()
	{	
		int watchesLeft = 5;
		while(watchesLeft > 0){
			Console.WriteLine("There are " + watchesLeft + " watches left"); 
			watchesLeft--;
		}
		Console.WriteLine("No watch left!"); 		
	}
}

This will produce following result

There are 5 watches left
There are 4 watches left
There are 3 watches left
There are 2 watches left
There are 1 watches left
No watch left!

Do While Loop

Do While loop looks similar to While loop but it is different loop as compared to while loop because here the given condition is checked at the bottom of the loop. It is to be noted that do while loop is guaranteed to be executed at least one time.

Syntax

do{
statement;
}
while(condition)

Do While Loop

Example

using System;
class DoWhileLoopDemo
{
	static void main()
	{	
		int watchesLeft = 5;
		do
		{
				Console.WriteLine("There are " + watchesLeft + " watches left"); 
				watchesLeft--;
		}
		while(watchesLeft > 0)
		Console.WriteLine("No watch left!"); 		
	}
}

This will produce following result

There are 5 watches left
There are 4 watches left
There are 3 watches left
There are 2 watches left
There are 1 watches left
No watch left!


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.