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.

308 APPENDIX B The basics of C<br />

Listing B.8<br />

Using a do-while loop to log an arbitrary number of messages<br />

int x = 0;<br />

do {<br />

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

x = n x 1;<br />

} while (x < 15);<br />

Notice that listings B.8 and B.6 are similar in style and behavior. In this example, the<br />

only difference is in the behavior that occurs when the value of variable x is modified<br />

so that it initially has a value of 15 or higher. If x is initially set to the value 16, for<br />

example, the do-while loop version emits one "Hello, World!", whereas the while<br />

loop version does not.<br />

B.4.3<br />

The for statement<br />

Although while and do loops are flexible, they aren’t the most concise of statements<br />

to understand at a glance. To determine how many times a given while loop may loop<br />

or under what conditions it will exit, you must look for logic potentially spread across<br />

a wide number of individual statements on a number of lines of source code.<br />

A for loop is designed to make the common scenario of looping a specific number<br />

of times easier, yet still provide a flexible construct capable of more complex<br />

looping conditions.<br />

Looking at the various code listings containing while and do loops, notice that<br />

they generally have three things in common:<br />

■<br />

■<br />

■<br />

A statement that initializes a variable to an initial value<br />

A condition that checks the variable to determine if the loop should continue<br />

looping<br />

A statement to update the value of the variable each time through the loop<br />

A for loop can specify all three components in a single, concise statement. The general<br />

form of a for loop is as follows:<br />

for (initialization_expr; loop_condition; increment_expr)<br />

{<br />

statement block;<br />

}<br />

The three expressions enclosed in parentheses specify the three components previously<br />

outlined and determine how many times the statements in the for loop will be executed.<br />

The flexibility of three individual expressions allows for a number of variations, but the<br />

most common arrangement is to configure a for loop to loop a fixed number of times.<br />

In this scenario, the initialization expression is executed once to set the initial<br />

value of a loop counter. The second component specifies the condition that must continue<br />

to hold true in order for the loop to continue executing, and the last component<br />

is an expression that’s evaluated at the end of each cycle through the loop to<br />

update the value of the loop counter.

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

Saved successfully!

Ooh no, something went wrong!