O'Reilly logo

Objective-C Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required