For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator

Embed Size (px)

Citation preview

  • for Repetition Structures (L13)

    * General Form of the for Statement* Components of a Typical for Header* Pre/Postincrement of the Counter* Stream Manipulator setw(m)* Case Study* Exercise/Home Work Dr. Ming Zhang

  • General Form of the for Statement (1)

    for header for keyword semicolon

    for (initializing list; expression; altering list) statement; for bodyfor (initializing list; expression; altering list){ statement; ....... statement; } Dr. Ming Zhang

  • Initializing List* Initializing ListThe initializing list consists of a single statement used to set the starting (initial value) of a counter.* Example (initializing list, starting from counter = 1)

    for(counter=1;counter

  • Expression* Expression The expression contains the maximum or minimum value the counter can have and determines when the loop is finished.* Example ( expression, if i > 15, loop is finished)

    for ( i=5; i

  • Altering List* Altering ListThe altering list provides the increment value that is added to or decrement value that is subtracted from the counter each time the loop is executed.* Example(altering list, j=j-1 each time the loop is executed)

    for (j=100; j > 1; j-- ) cout

  • Components of a Typical for HeaderKeyword Control Final value of variable control variable name for which the condition is true

    for ( counter =1; counter

  • Pre/Postincrement of the Counter* Postincrement of the Counterfor (int counter = 1; counter
  • A for Loop Flowchart Initializing Statement(s)

    Evaluate false the tested exit expression for Loop true Execute the statement after the parentheses

    Execute the altering list Dr. Ming Zhang

  • A Typical for Loop Flowchart counter = 1

    false counter

  • Expressions Equivalent* The increment expression in the for structure acts like a stand-along statement at the end of the body of the for. * Therefore, the expressions: counter = counter + 1counter += 1++ countercounter ++are equivalent in the incrementing portion of the for structure. Dr. Ming Zhang

  • Pre/Postincrement of the Counter* Postincrement of the Counterfor (int counter = 1; counter
  • Question1: Determine the value in total Exercise/Home Work(1) total = 1; for(count =1; count
  • Stream Manipulator setw(m)* The call setw(m) specifies that the next value output is printed in filed width of m. * If the value to be output is less than m character positions wide, the value is right justified in the filed by default.* If the value to be output is more than m character position wide, the field width is extended to accommodate the entire value. Dr. Ming Zhang

  • Case Study: Calculating Compound InterestFigure 2.21 (D & D)......for (int year =1; year
  • Question 2: Calculate the SalaryExercise/Home Work

    A programmer starts with a salary of $45,000 and expects to receive a $5,000 raise each year.Write a program to compute and print the programmers salary for each of the first 10 years and the total amount of money the programmer would receive over the 10-year period. Dr. Ming Zhang

  • Question 3: Fibonacci SequenceExercise/ Home Work The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21......., where the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms; that is Fib(n) = Fib(n-1) + Fib(n-2). Using this information, write a program that calculate the nth number in a Fibonancci sequence, where n is interactively entered into the program by the user. For example, if n = 6, the program should display the value 5. Dr. Ming Zhang