C Sharp Looping - The for Statement

From Techotopia
Revision as of 16:19, 17 January 2008 by Neil (Talk | contribs) (New page: In this chapter of C Sharp Essentials we will continue looking at flow control in C# applications. In the preceding chapters we have looked in detail at using logical expressions to de...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In this chapter of C Sharp Essentials we will continue looking at flow control in C# applications. In the preceding chapters we have looked in detail at using logical expressions to decide what C# code should be executed. Another aspect of flow control entails the definition of loops. loops are essentially sequences of C# statements which are to be executed repeatedly until specified condition (or conditions) are met.

It is generally common knowledge that computers are great at performing repetitive tasks an infinite number of times, and doing so very quickly. It is also common knowledge that computers really don't do anything unless someone programs them to tell them what to do. Loop statements are the primary mechanism for telling a computer to perform the same task over and over again until a set of crieria are met. This is where for, while and do ... while loops are of use. This chapter is dedicated to the design of for loops in C#. The while and do ... while' looping techniques will be covered in C# Looping with while and do while.


Contents


The C# for Loop

C# for loops are ideal in situations where the same sequence of statements need to repeated a specific number of times. Suppose, for example, that you have a requirement to add a number to itself ten times. One way to do this might be to write the following C# code:


int j = 10;

j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;

Whilst this is somewhat cumbersome it does work. What would happen, however, if you needed to perform this task 100 times or even 10,000 times. Writing C# code to perform this as above would be prohibitive. This is scenario is exactly what the for loop is intended to handle.

The syntax of a C# for loop is as follows:

for ( ''initializer''; ''conditional expression''; ''loop expression'' )
{
      statements to be executed
}

The initializer typically initializes a counter variable. Traditionally the variable i is used for this purpose. For example:

i = 0;

This sets the counter to be the variable i and sets it to zero. Note that if the count variable has not been previously declared it may be declared as part of the for statement:

int i = 0;

The conditional expression specifies the test to perform to verify whether the loop has been performed the required number of iterations. For example, if we want to loop 100 times:

i < 100;

Finally the loop expression specifies the action to perform on the counter variable. For example to increment by 1:

i++;

The body of statements to be executed on each iteration of the loop are contained within the code block defined by the opening ({) and closing (}) braces.

Bringing this all together we can create a for loop to perform the task outlined in the earlier example:


int j = 10;

for (int i=0; i<100; i++)
{
      j += j;
}

System.Console.WriteLine ("j = " + j);

Creating an Infinite for Loop

A for loop which will execute an infinite number of times may be construction using for (;;;) syntax. For example, the following code sample will output Hello from C# until the program is manually terminated by the user (or the computer is turned off or rebooted):

for (;;;)
{
    System.Console.WriteLine ("Hello from C#");
}

Breaking Out of a for Loop

Having created a loop it is possible that under certain conditions you might want to break out of the loop before the completion criteria have been met (and particularly if you have created an infinite loop). One such example might involve continually checking for activity on a network socket.

Nested for Loops

So far we have looked at only a single level of for loop. It is also possible to nest for loops where one for loop resides inside another for loop.