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 needed for Program 5.2. The method employed to calculate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access