Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Let’s take a look at a program that uses the for statement. The purpose of Program 5.2 is to calculate the 200th triangular number. See whether you can determine how the for statement works.
// Program to calculate the 200th triangular number
// Introduction of the for statement
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int n, triangularNumber;
triangularNumber = 0;
for ( n = 1; n <= 200; n = n + 1 )
triangularNumber += n;
NSLog (@"The 200th triangular number is %i", triangularNumber);
[pool drain];
return 0;
} |