C# Workshop – Loops

Standard

Loops

Another way you can do more things by repeating statements in a loop. The first type of loop is the while loop which is like the if statement but instead of performing its operation once it will perform it until the condition is satisfied.

To use a while loop in dotnetfiddle.net enter the following:

using System;

public class Demo
{	
	public static void Main()
	{
		int number = 0;
		Console.WriteLine("Enter Number between 1 and 10");
		while(number < 1 || number > 10)
		{
			number = int.Parse(Console.ReadLine());
		}	
		Console.WriteLine(number + " is valid");
	}
}

In this example, the while loop will continue until the number is lower than 1 or it is higher than 10, so if enter anything outside that range it will keep looping otherwise it will exit the loop, this is like if that the loop only happens as long as the condition is true.
It is possible to write a loop that checks the condition last with a do while loop, you can do this in dotnetfiddle.net by entering the following:

using System;

public class Demo
{	
	public static void Main()
	{
		int number = 0;
		Console.WriteLine("Enter Number between 1 and 10");
		do
		{
			number = int.Parse(Console.ReadLine());
		} while(number < 1 || number > 10);	
		Console.WriteLine(number + " is valid");
	}
}

Another kind of loop is the for loop which is ideal for use when you know how many times you want to loop or it’s easy to work out how many times the loop needs to run.

You can write a for loop in dotnetfiddle.net and enter the following:

using System;

public class Demo
{	
	public static void Main()
	{
		for(int number = 0; number <= 10; number++)
		{
			Console.WriteLine(number);	
		}
	}
}

The while loop uses a single bool expression but the for loop uses three expressions, the first declares an int variable called number and sets it to 0, the second is the conditional expression which will be true until the number is not less than or equal to 10, the third expression is used to increment the value using another Operator ++ which adds 1 to the value, there’s also that subtracts 1 from a value.

Another type of for loop is a foreach which will loop through an Array or similar Object where the items to be looped through already exist – an array is just a list of values and is denoted by [] to use this and the foreach in dotnetfiddle.net enter the following:

using System;

public class Demo
{	
	public static void Main()
	{
		int[] numbers = { 1, 2, 3, 4, 5 }; 
		foreach(int number in numbers)
		{
			Console.WriteLine(number);	
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s