Objective-C Looping - The for Statement

From Techotopia
Revision as of 14:04, 13 October 2009 by Neil (Talk | contribs)

Jump to: navigation, search

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


Contents


Why Use Loops?

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 that a sequence of tasks needs to be 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 Objective-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 or even 10,000 times? Writing Objective-C code to perform this as above would be prohibitive. Such a scenario is exactly what the for loop is intended to handle.

The syntax of an Objective-C for loop is as follows:

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

<google>ADSDAQBOX_FLOW</google> The initializer typically initializes a counter variable. Traditionally the variable name i is used for this purpose, though any valid variable name will do. For example:

i = 0;

This sets the counter to be the variable i and sets it to zero. Note that the current widely used Objective-C standard (c89) requires that this variable be declared prior to its use in the for loop. For example:

int i=0;

for (i = 0; i < 100; i++)
{
     // Statements here
}

The next standard (c99) allows the variable to be declared and initialized in the for loop as follows:

for (int i=0; i<100; i++)
{
    //Statements here
}

If you compile code containing this construct you may get an error similar to the following:

error: 'for' loop initial declaration used outside C99 mode

If you do see this message, you should be able to switch into C99 mode by adding the -std=c99 command-line flag to your compilation command. For example:

gcc -std=c99 -framework Foundation hello.m -o hello

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 is contained within the code block defined by the opening ({) and closing (}) braces. If only one statement is to be executed the braces are optional, though still recommended for code readability and so that you don’t forget the add them if you later increase the number of statements to be performed in the loop.

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

int j = 10;

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

NSLog (@"j = %i", j);

Objective-C Loop Variable Scope

A key point to note in creating loops is that any variables defined within the body of a loop are only visible to code within the loop. This is concept known as scope. If, for example, a variable myCounter is defined in a for loop, that variable ceases to exist once the loop terminates:

// variable myCounter does not yet exist

for (int i = 0; i < 10; i++)
{
       int myCounter = 0; //myCounter variable created in scope of for loop

       myCounter += i;
}

// after loop exit variable myCounter is now out of scope and ceases to exist

Creating an Infinite for Loop

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

for (;;)
{
    NSLog (@"Hello from Objective-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 (particularly if you have created an infinite loop). One such example might involve continually checking for activity on a network socket. Once activity has been detected it will be necessary to break out of the monitoring loop and perform some other task.

For the purpose of breaking out of a loop C# provides the break statement which breaks out of the current loop and resumes execution at the code directly after the loop. For example:

int j = 10;
int i;

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

     if (j > 100)
          break;

     NSLog (@"j = %i", j);
}

In the above example the loop will continue to execute until the value of j exceeds 100 at which point the loop will exit.

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. For example:

int j;
int i;

for (i = 0; i < 100; i++)
{
     NSLog( @"i = %i", i);

     for (j = 0; j < 10; j++)
     {
             NSLog ( @"j = %i", j);
     }
}

The above example will loop 100 times displaying the value of i on each iteration. In addition, for each of those iterations it will loop 10 times displaying the value of j.

Breaking From Nested Loops

An important point to be aware of when breaking out of a nested for loop is that the break only exits from the current level of loop. For example, the following Objective-C code example will exit from the current iteration of the nested loop when j is equal to 5. The outer loop will, however, continue to iterate and, in turn execute the nested loop:

for (i = 0; i < 100; i++)
{
     NSLog( @"i = %i", i);

     for (j = 0; j < 10; j++)
     {
             if (j == 5)
                   break;

             NSLog ( @"j = %i", j);
     }
}

Continuing for Loops

Another useful statement for use in loops in the continue statement. When the execution process finds a continue statement in any kind of loop it skips all remaining code in the body of the loop and begins execution once again from the top of the loop. Using this technique we can construct a for loop which outputs only even numbers between 1 and 9:

int j;
int i;

for (i = 1; i < 10; i++)
{
          if ((i % 2) != 0)
               continue;

          NSLog( @"i = %i", i);
}

In the example, if i is not divisible by 2 with 0 remaining the code performs a continue sending execution to the top of the for loop, thereby bypassing the code to output the value of i. This will result in the following output:

2
4
6
8

for Loops with Multiple Variables

In the examples we have covered so far we have used a single variable within the for loop construct. Objective-C actually permits multiple variables to be modified within the looping process. In the following example, the for loop increments two variables, i and j

int j;
int i;

for (j = 0, i = 0; i < 10; i++, j++)
{
          NSLog( @"i = %i, j = %i", i, j);
}

Note that although both i and j are initialized and incremented in this loop, the number of times the loop is to be performed is still based on the value of i through the i > 10 expression. The initialization and modification expressions for additional variables do not need to be the same as the control variable. For example, the following example initializes j to 5 multiplies it by 2:

int j;
int i;

for (j = 1, i = 0; i < 10; i++, j *= 2)
{
          NSLog( @"i = %i, j = %i", i, j);
}

When the above loop is executed we get output similar to:

2009-10-13 09:32:22.498 t[1867:10b] i = 0, j = 1
2009-10-13 09:32:22.500 t[1867:10b] i = 1, j = 2
2009-10-13 09:32:22.500 t[1867:10b] i = 2, j = 4
2009-10-13 09:32:22.501 t[1867:10b] i = 3, j = 8
2009-10-13 09:32:22.501 t[1867:10b] i = 4, j = 16
2009-10-13 09:32:22.502 t[1867:10b] i = 5, j = 32
2009-10-13 09:32:22.502 t[1867:10b] i = 6, j = 64
2009-10-13 09:32:22.503 t[1867:10b] i = 7, j = 128
2009-10-13 09:32:22.503 t[1867:10b] i = 8, j = 256
2009-10-13 09:32:22.503 t[1867:10b] i = 9, j = 512