O'Reilly logo

Programming in Objective-C, Sixth Edition by Stephen G. Kochan

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

The for Statement

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 5.2

// Program to calculate the 200th triangular number// Introduction of the for statement#import <Foundation/Foundation.h>int main (int argc, char * argv[]){   @autoreleasepool {      int n, triangularNumber;      triangularNumber = 0;      for ( n = 1; n <= 200; n = n + 1 )         triangularNumber += n;      NSLog (@"The 200th triangular number is %i", triangularNumber);   }   return 0;}

Program 5.2 Output

The 200th triangular number is 20100

Some explanation is ...

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