Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator."— Presentation transcript:

1 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

2 General Form of the for Statement (1) for header for keyword semicolon for (initializing list; expression; altering list) statement; for body for (initializing list; expression; altering list){ statement;....... statement; } Dr. Ming Zhang

3 Initializing List * Initializing List The 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<100;counter=counter+1) cout << counter << endl; Dr. Ming Zhang

4 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 <= 15; i = i+2) cout << i << endl; Dr. Ming Zhang

5 Altering List * Altering List The 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 << j<< endl; Dr. Ming Zhang

6 Components of a Typical for Header Keyword Control Final value of variable control variable name for which the condition is true for ( counter =1; counter <= 7, ++counter) Initial value of Loop- Increment control variable continuation of control condition variable Dr. Ming Zhang

7 Pre/Postincrement of the Counter * Postincrement of the Counter for (int counter = 1; counter <= 10; counter ++) cout << counter ; Output: 12345678910 * Preincrement of the Counter for (int counter = 1; counter <= 10; ++counter) cout << counter ; Output: (1?)2345678910(11?) Dr. Ming Zhang

8 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

9 A Typical for Loop Flowchart counter = 1 false counter <=10? exit for Loop true cout << counter; counter++ or ++counter Dr. Ming Zhang

10 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 + 1 counter += 1 ++ counter counter ++ are equivalent in the incrementing portion of the for structure. Dr. Ming Zhang

11 Pre/Postincrement of the Counter * Postincrement of the Counter for (int counter = 1; counter <= 10; counter ++) cout << counter ; Output: 12345678910 * Preincrement of the Counter for (int counter = 1; counter <= 10; ++counter) cout << counter ; Output: 12345678910 !!!!!!!!!!!!!!!!!!!!!!! Dr. Ming Zhang

12 Question1: Determine the value in total Exercise/Home Work (1) total = 1; for(count =1; count <=10; count++) total = total *2; (2) total = 50; for(i=9; i <12; ++i) total = total - i; (3) total = 64; for(j=1; j <= 6; j += 1) total = total/2; Dr. Ming Zhang

13 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

14 Case Study: Calculating Compound Interest Figure 2.21 (D & D)...... for (int year =1; year <=10; year++){ amount=principal * pow(1.0+rate, year); cout<< setw(4) << year <<setw(21) << amount << endl; }...... output year Amount on deposit 1 1050.00.............................................. 10 1628.89 Dr. Ming Zhang

15 Question 2: Calculate the Salary Exercise/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 programmer’s 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

16 Question 3: Fibonacci Sequence Exercise/ 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


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

Similar presentations


Ads by Google