Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 4 - Loops


A loop is something that is used to repeat a section of code as many times as you want. If you wanted to print Hello 100 times then you could use 100 lines of code or you can do the same in only a few lines using a loop.

For Loop

The for loop counts from one number to another number. You start a for loop with the for keyword which is followed by brackets. The first section inside the brackets is where you set the starting value of the loop counter variable. The second section is the loop end condition. The third is where the loop counter is incremented. The code to run for the loop goes after the brackets.

Here is an example of how to repeat printing Hello 5 times. First we declare a loop counter variable called x. At the for loop we initialize the loop counter to 1. The loop will keep repeating while x is less than or equal to 5. The x++ increments x by 1 each time the loop repeats. x-- would be used if you wanted to decrement. You can also use x = x + 1 instead of x++ if you really want to.
int x;
for (x = 1; x <= 5; x++)
   Console.WriteLine("Hello");

It is possible to declare the loop variable inside the loop variable initialization part of the for loop and it is also a good idea to do this.
for (int x = 1; x <= 5; x++)
   Console.WriteLine("Hello");

While Loop

A while loop repeats itself while its condition is true. You need to use your own loop counter in a while loop which means you must increment it inside the loop each time. A while loop starts with the while keyword. This is followed by the condition in brackets. The loop code goes after this. If you want to use more than 1 command inside any loop you must surround them with curly brackets. Here is an example that prints Hello 5 times.
int y = 1;
while (y <= 5)
{
   Console.WriteLine("Hello");
   y++;
}

Do While Loop

The do while loop is similar to the while loop except that the condition is tested at the bottom of the loop. You start a do while loop with the do keyword. The while goes after the loop code. You must remember to put a semi-colon after the while at the bottom. We will look at a different example this time that keeps asking the user to enter a password until he gets it correct.
string password;
do
{
   Console.WriteLine("Please enter the password: ");
   password = Console.ReadLine();
}
while (password != "abc");

break and continue

You can use the break command to exit a loop at any time. For the following example we will create an infinite loop. This loop would usually print Hello forever but we will use the break command to stop it after it has printed Hello once.
while (1 == 1)
{
   Console.WriteLine("Hello");
   break;
}

The continue command will jump ahead to the next iteration of the loop. The following example will not print Hello because the continue command sends control back to the top of the loop each time.
for (int z = 1; z <= 5; z++)
{
   continue;
   Console.WriteLine("Hello");
}

0 comments:

Post a Comment

Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India