COSC 181 Foundations of Computer Programming Class 16 5 3 for Repetition Statement Cont General form of the for statement for initialization loopContinuationCondition increment statement s Can usually be rewritten as initialization while loopContinuationCondition statement s increment If the control variable is declared in the initialization expression It will be unknown outside the for statement 2 Good Programming Practice 5 4 Using the final value in the condition of a while or for statement and using the relational operator will help avoid off by one errors For a loop used to print the values 1 to 10 which is better Hint one is an error counter 10 counter 10 counter 11 Many programmers prefer so called zero based counting in which to count 10 times through the loop counter would be initialized to zero and the loop continuation test would be counter 10 3 Good Programming Practice 5 5 Place only expressions involving the control variables in the initialization and increment sections of a for statement Manipulations of other variables should appear either before the loop if they should execute only once like initialization statements or in the loop body if they should execute once per repetition like incrementing or decrementing statements 4 5 3 for Repetition Statement Cont The initialization and increment expressions can be comma separated lists of expressions These commas are comma operators Comma operator has the lowest precedence of all operators Expressions are evaluated from left to right Value and type of entire list are value and type of the rightmost expressions 5 5 4 Examples Using the for Statement for statement examples Vary control variable from 1 to 100 in increments of 1 for int i 1 i 100 i Vary control variable from 100 to 1 in increments of 1 for int i 100 i 1 i Vary control variable from 7 to 77 in steps of 7 for int i 7 i 77 i 7 Vary control variable from 20 to 2 in steps of 2 for int i 20 i 2 i 2 Vary control variable over the sequence 2 5 8 11 14 17 20 for int i 2 i 20 i 3 Vary control variable over the sequence 99 88 77 66 55 44 33 22 11 0 for int i 99 i 0 i 11 6 1 Fig 5 5 f ig05 05 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl Summing integers with the for statement 6 7 int main 8 9 int total 0 initialize total 10 11 total even integers from 2 through 20 12 for int number 2 number 20 number 2 13 total number 14 15 cout Sum is total endl display results 16 return 0 successful termination Outline fig05 0 Vary number from 2 to 20 in steps of 2 5 cpp 1 of 1 Add the current value of number to total 17 end main Sum is 110 7 5 4 Examples Using the for Statement Cont Using a comma separated list of expressions Lines 12 13 of Fig 5 5 can be rewritten as for int number 2 initialization number 20 loop continuation condition total number number 2 total and increment empty statement 8 5 4 Examples Using the for Statement Cont Standard library function std pow Calculates an exponent Example pow x y Calculates the value of x raised to the yth power Requires header file cmath 9 1 2 3 4 Fig 5 6 f ig05 06 cpp Compound in te res t calcu la t io ns with for inc lude iostream us ing std cout 5 6 7 8 9 10 11 12 13 14 us ing std endl us ing std fixed setw stream manipulator will set a field width inc lude iomanip us ing std setw enables program to set a field width standard us ing std setprecision library function pow in header file cmath inc lude cmath standard C math library us ing std pow enables program to use function pow Outline fig05 0 6 cpp 1 of 2 C treats floating point values as type double 15 int main 16 17 double amount amount on deposit at end of each year 18 double principal 1000 0 initial amount before interest Specify that the next value output 19 double rate 05 interest rate should appear in a field width of 21 20 21 display headers 22 cout Year setw 21 Amount on deposit endl 23 24 set floating point number format 25 cout fixed setprecision 2 26 10 27 calculate amount on deposit for each of ten years 28 for int year 1 year 10 year 29 30 calculate new amount for specified year 31 amount principal pow 1 0 rate year Calculate amount within for statement fig05 0 32 33 display the year and the amount 34 cout setw 4 year setw 21 amount endl 35 end for 36 37 return 0 indicate successful termination 38 end main Year 1 2 3 4 5 6 7 8 9 10 Outline Use the setw stream manipulator to set field width 6 cpp 2 of 2 Amount on deposit 1050 00 1102 50 1157 63 1215 51 1276 28 1340 10 1407 10 1477 46 1551 33 1628 89 11 Common Programming Error 5 7 In general forgetting to include the appropriate header file when using standard library functions e g cmath in a program that uses math library functions is a compilation error 12 5 4 Examples Using the for Statement Cont Formatting numeric output Stream manipulator setw Sets field width Right justified by default Stream manipulator left to left justify Stream manipulator right to right justify Applies only to the next output value Stream manipulators fixed and setprecision Sticky settings Remain in effect until they are changed 13 Performance Tip 5 1 Avoid placing expressions whose values do not change inside loops but even if you do many of today s sophisticated optimizing compilers will automatically place such expressions outside the loops in the generated machine language code 14 5 5 do while Repetition Statement do while statement Similar to while statement Tests loop continuation after performing body of loop Loop body always executes at least once 15 Good Programming Practice 5 9 Always including braces in a do while statement helps eliminate ambiguity between the while statement and the do while statement containing one statement 16 1 Fig 5 7 f ig05 07 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl do whi le 6 7 int main 8 9 int counter 1 initialize counter 10 11 do 12 13 cout counter display counter 14 counter increment counter 15 Outline repet it io n statement Declare and initialize control variable counter fig05 0 7 cpp do while loop displays counter s value before testing for counter s final value 1 of 1 while counter 10 end do while 16 17 cout endl output a newline 18 return0 indicate successful termination 19 end main 1 2 3 4 5 6 7 8 9 10 17 UML activity diagram for do while 18 5 6 switch Multiple Selection …
View Full Document