Saturday, 31 December 2011

The for Repetition Statement





  •  Format when using for loops
    • for ( initialization; loopContinuationTest; increment )statement
  •  Example:
    • for(counter = 1; counter <= 10; counter++ ) (No semicolon (;)
      after last expression)
      • printf( "%d\n", counter );
  •  Prints the integers from one to ten 

  • For loops can usually be rewritten as while loops:
    • initialization;
    • while ( loopContinuationTest ) {
      • statement;
      • increment;
      • }
  •  Initialization and increment
    • Can be comma-separated lists
    •  Example:
      • for ( i = 0, j = 0; j + i <= 10; j++, i++)
        • printf( "%d\n", j + i );


Flowcharting a typical for repetition statement


example...


result...




No comments:

Post a Comment