01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Looping statements<br />

305<br />

Not as straightforward is the Kitten case, which doesn’t end in a break statement.<br />

If the switch statement is executed while the pet variable stores the value Kitten, the<br />

first call to NSLog is executed, and execution proceeds to the next statement. Because<br />

the Kitten case has no additional statements, execution “falls through” into the Cat<br />

case block, and the second call to NSLog is executed.<br />

It’s not necessary for a case block to have any statements, as in Horse case: notice it<br />

immediately falls through into the case for Pony. This feature is handy when multiple<br />

values should be treated identically.<br />

With your newfound knowledge of conditional statements, you can alter the<br />

sequence of operations your applications perform. And the C developer’s toolbox has<br />

even more tricks. As in real life, the path an iPhone application executes is not<br />

straight; it can contain many twists and turns. You may want to repeat a set of statements<br />

or continue doing something until a certain condition becomes true, such as a<br />

user pressing a button or an array running out of data. Looping statements with keywords<br />

such as for and while are a natural progression from conditional statements<br />

and enable an application to repeat a set of statements.<br />

B.4 Looping statements<br />

In your application logic you may come across the need to repeat a particular statement<br />

a fixed number of times. In chapter 1 you printed "Hello, World!" to the<br />

debug console with the following statement:<br />

NSLog(@"Hello, World!");<br />

If you wanted to say "Hello, World!" three times in a row, you might be tempted to<br />

add two calls to NSLog:<br />

NSLog(@"Hello, World!");<br />

NSLog(@"Hello, World!");<br />

NSLog(@"Hello, World!");<br />

Using an if statement, it’s possible to extend this approach to make the number of<br />

times you print "Hello, World!" conditional on the value stored in a variable. For<br />

example, the code in the following listing uses the value of variable n to determine<br />

how many times to print "Hello, World!" to the console (between 0 and 3 times).<br />

Listing B.5<br />

Controlling how many times a message is logged based on program state<br />

int n = 2;<br />

if (n > 0)<br />

NSLog(@"Hello, World!");<br />

if (n > 1)<br />

NSLog(@"Hello, World!");<br />

if (n > 2)<br />

NSLog(@"Hello, World!");<br />

While this technique is perfectly acceptable, it may not be the best or most scalable<br />

approach. As an example, try to extend this technique to print "Hello, World!"

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!