Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24COSC 181 – Foundations of Computer ProgrammingClass 154.11 Assignment OperatorsAssignment expression abbreviationsAddition assignment operatorExample•c = c + 3; abbreviates to c += 3;Statements of the formvariable = variable operator expression;can be rewritten asvariable operator= expression;Other assignment operators•d -= 4 (d = d - 4)•e *= 5 (e = e * 5)•f /= 3 (f = f / 3)•g %= 9 (g = g % 9)Fig. 4.19 | Arithmetic assignment operators. AssignmentoperatorSampleexpressionExplanation AssignsAssume: int c = 3, d = 5, e = 4, f = 6, g = 12; + = c + = 7 c = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f % = g % = 9 g = g % 9 3 to g4.12 Increment and Decrement OperatorsIncrement operator ++ Increments variable by oneExample•c++Decrement operator -- Decrement variable by oneExample•c--4.12 Increment and Decrement Operators (Cont.)PreincrementWhen the operator is used before the variable (++c or --c)Variable is changed, then the expression it is in is evaluated using the new valuePostincrementWhen the operator is used after the variable (c++ or c--)Expression the variable is in executes using the old value, then the variable is changedFig. 4.20 | Increment and decrement operators. Operator CalledSampleexpressionExplanation + + preincrement+ + a Increment a by 1, then use the new value ofa in the expression in which a resides. + + postincrementa+ +Use the current value of a in the expressionin which a resides, then increment a by 1. -- predecrement--b Decrement b by 1, then use the new valueof b in the expression in which b resides. -- postdecrementb --Use the current value of b in the expressionin which b resides, then decrement b by 1.Good Programming Practice 4.8 Unlike binary operators, the unary increment and decrement operators should be placed next to their operands, with no intervening spaces.1 // Fig. 4.21: f ig04_21.cpp2 / / Preinc rement ing and post inc rement ing.3 #inc lude < iostream >4 us ing std::cout;5 us ing std::endl;67 int m ain()8 {9 int c;1011 / / demonstrate post increment12 c = 5; // assign 5 to c13 cout < < c < < endl; // print 514 cout < < c+ + < < endl; // print 5 then postincrem ent15 cout < < c < < endl; // print 6 1617 cout < < endl; // skip a line1819 // dem onstrate preincrem ent20 c = 5; // assign 5 to c 21 cout < < c < < endl; // print 5 22 cout < < + + c < < endl; // preincrem ent then print 623 cout < < c < < endl; // print 624 return 0; // indicate successful term ination25 } // end main556566Outlinefig04_21.cpp (1 of 1)Postincrementing the c variablePreincrementing the c variable4.12 Increment and Decrement Operators (Cont.)If c = 5, then •cout << ++c; •c is changed to 6Then prints out 6•cout << c++; Prints out 5 (cout is executed before the increment) •c then becomes 64.12 Increment and Decrement Operators (Cont.)When variable is not in an expressionPreincrementing and postincrementing have same effectExample• ++c; cout << c;and c++; cout << c;are the sameCommon Programming Error 4.14 4Attempting to use the increment or decrement operator on an expression other than a modifiable variable name or reference, e.g., writing ++(x + 1), is a syntax error.Fig. 4.22 | Operator precedence for the operators encountered so far in the text. Operators Associativity Type () left to right parentheses + + -- static_cast< type > () left to right unary (postfix) + + -- + - right to left unary (prefix) * / % left to right multiplicative + - left to right additive < < > > left to right insertion/extraction < < = > > = left to right relational = = !=left to right equality ?: right to left conditional = + = -= *= /= % =right to left assignment13Chapter 5Topics coveredReview of the essentials of counter-controlled repetition.Introduction to the for and do…while repetition statements.Introduction to the switch selection statement.The use of the break and continue program control statements to alter the flow of control.The use of the logical operators to form complex conditional expressions in control statements.Avoiding the consequences of confusing the equality and assignment operators.145.2 Essentials of Counter-Controlled RepetitionCounter-controlled repetition requires:Name of a control variable (loop counter)Initial value of the control variableLoop-continuation condition that tests for the final value of the control variableIncrement/decrement of control variable at each iteration151 // Fig. 5.1: f ig05_01.cpp2 / / Counter - contro l led repet it io n.3 #include < iostream >4 us ing std::cout;5 us ing std::endl;67 int m ain()8 {9 int counter = 1; // declare and initialize control variable1011 while ( counter < = 10 ) // loop-continuation condition12 { 13 cout < < counter < < " ";14 counter+ + ; // increm ent control variable by 115 } // end w hile 1617 cout < < endl; // output a new line18 return 0; // successful term ination19 } // end main1 2 3 4 5 6 7 8 9 10Outlinefig05_01.cpp (1 of 1)Control-variable name is counter with variable initial value 1Condition tests for counter’s final value Increment the value in counter16Common Programming Error 5.1 Floating-point values are approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination. Control counting loops with integer values.17Good Programming Practice 5.2 Too many levels of nesting can make a program difficult to understand. As a rule, try to avoid using more than three levels of indentation.18Good Programming Practice 5.3 Vertical spacing above and below control statements and indentation of the bodies of control statements within the control statement headers give programs a two-dimensional appearance that greatly improves readability.195.3 for Repetition Statementfor repetition statementSpecifies counter-controlled repetition details in a single line of code201 // Fig. 5.2: f ig05_02.cpp2 / /
View Full Document