DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COSC 181 Foundations of Computer Programming Class 15 4 11 Assignment Operators Assignment expression abbreviations Addition assignment operator Example c c 3 abbreviates to c 3 Statements of the form variable variable operator expression can be rewritten as variable operator expression Other assignment operators d e f g 4 5 3 9 d e f g d e f g 4 5 3 9 Fig 4 19 Arithmetic assignment operators Assignment operator Sample expression Explanation Assigns Assume 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 g 4 12 Increment and Decrement Operators Increment operator Increments variable by one Example c Decrement operator Decrement variable by one Example c 4 12 Increment and Decrement Operators Cont Preincrement When the operator is used before the variable c or c Variable is changed then the expression it is in is evaluated using the new value Postincrement When the operator is used after the variable c or c Expression the variable is in executes using the old value then the variable is changed Fig 4 20 Increment and decrement operators Operator Called Sample expression Explanation preincrement a Increment a by 1 then use the new value of a in the expression in whicha resides postincrement a Use the current value of a in the expression in which a resides then increment a by 1 predecrement b Decrement b by 1 then use the new value of b in the expression in whichb resides postdecrement b Use the current value of b in the expression in 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Fig 4 21 f ig04 21 cpp Pre inc rement ing and post inc rement ing inc lude iostream us ing std cout us ing std endl Outline in t m ain fig04 2 int c demonstrate post inc rement c 5 assign 5 to c cout c endl print 5 cout c endl print 5 then postincrem ent cout c endl print 6 cout endl skip a line dem onstrate preincrem ent c 5 assign 5 to c cout c endl print 5 cout c endl preincrem ent then print 6 cout c endl print 6 24 return0 indicate successfulterm ination 25 end main 5 5 6 5 6 6 Postincrementing the c variable Preincrementing the c variable 1 cpp 1 of 1 4 12 Increment and Decrement Operators Cont If c 5 then cout c c is changed to 6 Then prints out 6 cout c Prints out 5 cout is executed before the increment c then becomes 6 4 12 Increment and Decrement Operators Cont When variable is not in an expression Preincrementing and postincrementing have same effect Example c cout c and c cout c are the same Common Programming Error 4 14 Attempting 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 4 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 assignment Chapter 5 Topics covered Review 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 13 5 2 Essentials of CounterControlled Repetition Counter controlled repetition requires Name of a control variable loop counter Initial value of the control variable Loop continuation condition that tests for the final value of the control variable Increment decrement of control variable at each iteration 14 1 Fig 5 1 f ig05 01 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl 6 7 in t m ain 8 9 Outline Counter contro l le d repet it io n Control variable name is counter with variable initial value 1 int counter 1 declare and initialize controlvariable 10 11 while counter 10 loop continuation condition 12 13 cout counter 14 counter increm ent controlvariable by 1 15 fig05 0 1 cpp Condition tests for counter s final value 1 of 1 end w hile 16 17 cout endl output a new line 18 return0 successfulterm ination Increment the value in counter 19 end main 1 2 3 4 5 6 7 8 9 10 15 Common Programming Error 5 1 Floating point values are approximate so controlling counting loops with floatingpoint variables can result in imprecise counter values and inaccurate tests for termination Control counting loops with integer values 16 Good 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 17 Good 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 twodimensional appearance that greatly improves readability 18 5 3 for Repetition Statement for repetition statement Specifies counter controlled repetition details in a single line of code 19 1 Fig 5 2 f ig05 02 cpp 2 Counter contro l le d repet it io n with the for statement 3 inc lude iostream 4 us ing std cout 5 us ing std endl Outline fig05 0 6 7 in t m ain 8 9 for statem ent header includes initialization 10 loop continuation condition and increm ent 11 for int counter 1 counter 10 counter 12 2 cpp 1 cout counter 13 14 cout endl output a new line 15 return 0 indicate successfulterm ination 16 end main 1 2 3 4 5 6 7 8 9 10 of 1 Increment for counter Condition tests for counter s final value Control variable name is counter with initial value 1 20 Common Programming Error 5 2 Using an incorrect relational operator or using an incorrect final value of a loop counter in the condition of a while or for statement can cause off by one errors 21 for statement header components Fig 5 3 22 Key Trick Example Possible to read in how many loops should be made int numberItems 0 cout Enter in the number of items cin count for int count numberItems count 0 count statement s This …


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?