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

Get Programming in Objective-C, Sixth Edition 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.