Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Hour 3. Introducing Objective-C > Controlling Program Flow

Controlling Program Flow

So far everything you’ve written has been linear. Each line of code you wrote executed immediately after the preceding line and it executed only once. What if you want to execute the same block of code 50 times? What if you want to execute a block of code over and over until you reach the end of an input file? What if you want to decide whether to execute a block of code based on some condition?

For these and many more scenarios, you have to turn to some Objective-C keywords that control your program’s execution flow. In short, you will need to use looping or logic keywords.

The most common way to control program flow is through conditional branching. This can be done with the if statement and the else statement or the highly favored switch statement.

An if statement works by evaluating some Boolean expression (an expression that yields true or false or, in C terms, either zero or nonzero for false and true, respectively). If the Boolean expression evaluates to true, the body of the if statement is executed. If the expression evaluates to false, the body of the if statement is not executed and, optionally, an if else or else block is executed.

Follow these steps to experiment with branching and looping:

1. Enter the following code on a blank line below the code you’ve been writing so far:

if ([favoriteColor isEqualToString:@"blue"])
{
    NSLog(@"Your favorite color is BLUE!");
}
else if ([favoriteColor isEqualToString:@"purple"])
{
    NSLog(@"You're into purple.");
}
else
{
    NSLog(@"You don't like blue or purple!");
}

Remembering back to earlier in the hour, we set the value of favoriteColor to “green”. This means that when you execute this code, it will not execute the body of the if statement or the body of the else if statement. It will execute the body of the else statement. Play around with the value of favoriteColor to get the different statements to execute. Note that every single Boolean expression evaluated by an if or else if statement must be wrapped in parentheses.

Next, let’s see what a simple for loop looks like. As you may have guessed, an Objective-C for loop works the same as it does in ANSI C.


By the Way

Later in the book you’ll see a special type of for loop called a fast iterator. This is an elegant, clean syntax that lets you iterate through the items in a collection with extremely good performance.


2. Enter the following code where you left off:

for (int x=0; x<10; x++)
{
    switch (x) {
        case 0:
            NSLog(@"First time through!");
            break;
        case 1:
        case 2:
        case 3:
        case 4:
            NSLog(@"Not halfway there yet...");
            break;
        default:
            NSLog(@"Working on the %dth iteration.", x);
            break;
    }
}

When executed, this produces output similar to the following:

2011-05-29 17:14:07.857 Hour3[1594:903] First time through!
2011-05-29 17:14:07.858 Hour3[1594:903] Not halfway there yet...
2011-05-29 17:14:07.859 Hour3[1594:903] Not halfway there yet...
2011-05-29 17:14:07.860 Hour3[1594:903] Not halfway there yet...
2011-05-29 17:14:07.861 Hour3[1594:903] Not halfway there yet...
2011-05-29 17:14:07.861 Hour3[1594:903] Working on the 5th iteration.
2011-05-29 17:14:07.862 Hour3[1594:903] Working on the 6th iteration.
2011-05-29 17:14:07.863 Hour3[1594:903] Working on the 7th iteration.
2011-05-29 17:14:07.864 Hour3[1594:903] Working on the 8th iteration.
2011-05-29 17:14:07.865 Hour3[1594:903] Working on the 9th iteration.

You can see that the for loop we just executed gave us 10 iterations, with indices ranging from 0 to 9. Other kinds of loops are available to us as well, including the do loop and the while loop. These loops both execute their bodies until the associated Boolean expression evaluates to false. The only difference is that a do loop will always execute at least once because it evaluates the Boolean condition after the body executes. A while loop evaluates the Boolean condition before the body executes, and if the expression evaluates to false, the body will never execute. Which loop you choose is entirely up to you and should be based on how readable you want the code to be and, of course, personal preference.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint