Tuesday, October 6, 2009

Objective-C Tuesdays: The for loop

Welcome to the first episode of Objective-C Tuesdays. We'll be focusing on C and Objective-C basics. Today we will look at the standard C language for loop.

The for loop is the most flexible looping statement in C, and the most common. It groups all the important loop information together at the start of the loop, making it quick and easy to both read and write.

The for loop statement starts with the keyword for, followed by parentheses containing the loop definition. The loop definition contains three expressions separated by semicolons: initialization, condition and action. After the parentheses, a single statement or block makes up the body of the loop.
/* for loop with a single statement */
for (initialization; condition; action) statement;

/* for loop with a block */
for (initialization; condition; action)
{
  block;
}
Watch out for a hard to spot bug when the for loop is followed by an empty statement created by an extra semicolon:
for (initialization; condition; action); /* <-- warning! empty statement! */
{
  /* this block is not part of the loop */
  /* it's always executed exactly once  */
}

Initialization The initialization expression is where the starting value of the loop counter variable is set. The initialization expression is evaluated once before the for loop begins executing. Loop counters are commonly integer types, but can be any type that makes sense in your code. In modern versions of C and Objective-C, the loop counter can be declared in the initialization section as well. The loop counter is typically given the name i (or j or k in nested loops). In C99 and later, initialization looks like:
for (int i = 0; condition; action)
In older versions of C, the loop counter must be declared somewhere before the for loop:
int i;
/* ... */
for (i = 0; condition; action)

Condition The condition expression is evaluated before each iteration; the loop continues while the condition evaluates to true or non-zero and stops when the condition evaluates to false or zero. The condition expression is often a "less than" (<) or "less than or equal to" (<=) expression:
for (int i = 0; i < 10; action)
or
for (int i = 1; i <= 10; action)
but it can be any expression that makes sense in your code.

Action The action expression is evaluated at the end of each iteration, after the condition and the body of the loop are executed. The action is typically used to increment or decrement the loop counter, but can be used to perform any action that makes sense for your code. Most of the time, you simply want to increment the counter by one:
for (int i = 0; i < 10; i++)
but other increments are easy to do:
for (int i = 0; i < 10; i += 2)

Example Putting this all together, here's some Objective-C code to count from 0 to 5 and back to zero:
for (int i = 0; i <= 5; i++) {
  NSLog(@"%i", i);
}
for (int i = 4; i >= 0; i--) {
  NSLog(@"%i", i);
}
The output looks like this:
0
1
2
3
4
5
4
3
2
1
0

Next week, we'll look at some of the crazy and complicated ways you can use the for loop.

3 comments:

Pavel Gnatyuk (Павел Гнатюк) said...

Great!
It is a simple and well-known subject, but I've never seen such great explanation.

billyboobob2004 said...

Nice explanation. I've seen plenty of them, and used (borrowed) plenty, but never understood the "exact" mechanics of it.

BTW... A minor typo... You say "here's some Objective-C code to count from 0 to 10 and back to zero:", but it counts up to 5 and back to zero. Oops. :)

Don McCaughey said...

Thanks billy! I'm glad you paying attention -- typo fixed :-)