CHAPTER 10

image

For Loops

For Loops Defined

Loops are used when you want to repeat a similar type of task many times. For loops are used when you know beforehand how many times you want to repeat a similar line of code. Here is a for loop that will write to the console window 10 times:

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

This for loop will produce this output:

i = 0i = 1i = 2i = 3i = 4i = 5i = 6i = 7i = 8i = 9

Let’s take a closer loop at the parts of this loop. The first thing is the for keyword. This lets the compiler know that you are coding a for loop.

Next, you have a series of code lines enclosed in parenthesis: (int i=0; i<10; ...

Get Objective-C Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.