01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Summary<br />

311<br />

CONTINUE<br />

The continue statement can be used as a shortcut to cause the current iteration of the<br />

loop to be prematurely ended, in effect causing the loop condition to immediately be<br />

reevaluated. For example, the following listing demonstrates a loop that skips over any<br />

numbers greater than 5.<br />

Listing B.11<br />

Stopping the current execution of a loop early with continue<br />

int numbers[11] = {1, 10, 0, 2, 9, 3, 8, 4, 7, 5, 6};<br />

int n = 0, sum = 0;<br />

do{<br />

if (numbers[n] > 6)<br />

continue;<br />

sum = sum + numbers[n];<br />

} while (n++ < 11 && sum < 15);<br />

NSLog(@"It takes the first %d numbers to get the sum %d", n, sum);<br />

The loop condition causes the loop to repeat while n is less than 11 (indicating the last<br />

number in the numbers array hasn’t been passed) and the sum is currently smaller<br />

than 15.<br />

Each iteration through the loop adds the next number to the running sum. But<br />

the number is first checked if it’s greater than 6, and if so, the continue statement<br />

skips the addition and immediately processes the next number in the numbers array.<br />

Notice the increment of the loop counter variable, n, was moved out of the loop<br />

and into the loop condition expression. If it hadn’t been moved but instead was incremented<br />

similarly to previous code samples, it would’ve become stuck after the first<br />

number greater than 6 was found. In this situation, the number would’ve caused the<br />

continue statement to be executed, skipping the rest of the statements in the loop,<br />

but nothing would’ve incremented the value of n to cause the next iteration through<br />

the loop to look at the next number in the array.<br />

It’s important to also note that the break and continue statements can exit only<br />

out of the immediate while, do, or for loop in which they’re contained. It’s not possible<br />

to break out of multiple loops if more than one loop is nested inside of another.<br />

B.5 Summary<br />

Because <strong>Objective</strong>-C is a strict superset of C, it’s important to have a strong understanding<br />

of the principles behind C before branching out to learn about the additions<br />

<strong>Objective</strong>-C brings to the table. This appendix offers you a firm foundation on which<br />

to start your <strong>Objective</strong>-C learning. It’s in no way a complete coverage of C-based programming,<br />

though, and many details have been left out.<br />

It’s clear that <strong>Objective</strong>-C owes a great debt to the C programming language. With<br />

C-based knowledge, you can perform calculations, make decisions, and alter the flow<br />

of execution throughout your applications. <strong>Objective</strong>-C expands upon the C foundations<br />

with its own flavor of object-oriented extensions.

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

Saved successfully!

Ooh no, something went wrong!