Download presentation
Presentation is loading. Please wait.
Published byMay Robbins Modified over 9 years ago
2
Chapter 3.1 & 3.2 s Programming s Assignment Statements s Incrementing & Decrementing s Math Library Functions
3
Review: Assignment Operator The assignment operator (=)causes the operand on the left to take on the value to the right side of the statement. This operator assigns from right to left. Syntax: variable = value validinvalid riker = 5.6 5 = riker *
4
Assignment Example 1 #include #include Using namespace.std; void main(void) { int sum; int sum; sum = 25;//initialize sum sum = 25;//initialize sum cout << “The number stored in sum is " cout << “The number stored in sum is " << sum; << sum; sum = sum + 10; sum = sum + 10; cout << "\nThe number now stored in sum is " << sum<< ‘\n’; cout << "\nThe number now stored in sum is " << sum<< ‘\n’; } sum 25 35
5
Example 1: Output Output: The number stored in sum is 25 The number now stored in sum is 35 No surprises
6
Assignment Example 2 int sum; sum = 0; cout << "\nThe value of sum is initially set to " << sum; sum = sum + 10; cout << "\nsum is now " << sum; sum = sum + 20; cout << "\nsum is now " << sum; sum = sum - 30; cout << "\nsum is now " << sum; sum = sum - 40; cout << "\nThe final sum is " << sum;
7
Assignment Example 2 int sum; sum = 0;// initialize sum cout << "\nThe value of sum is initially set to " << sum; sum = sum + 10; cout << "\nsum is now " << sum; sum = sum + 20; cout << "\nsum is now " << sum; sum = sum - 30; cout << "\nsum is now " << sum; sum = sum - 40; cout << "\nThe final sum is " << sum; Sumcout ??? 0 10 30 0 -40 A Trace of Ex2
8
Example 2 - Output Output: The value of sum is initially set to 0 sum is now 10 sum is now 30 sum is now 0 The final sum is -40 Hopefully, no surprises here either
9
Assignment Operators A shorthand notation for certain assignments. They all have right-to-left associativity. MyVariable += TaxRate * Cost variable op= (expression) is equivalent to variable = variable op (expression) Surprise! MyVariable = MyVariable + TaxRate * Cost
10
+=add then assign - =subtract then assign *=multiply then assign /=divide then assign %=modulus, then assign X - = 3 X = X - 3 pay *= 0.35 pay = pay * 0.35 Assignment Operators
11
Assignment Operators += - = * = /= %= 1. i += 2i = i + 2 2. r * = 7r = r * 7 3. j * = (k + 3)j = j * (k + 3) 4. x /= y - 4x = x /y - 4 5. hour %= 12hour = hour % 12 6. left - = t_outleft = left - t_out Assignment Operators *
12
Common Use Accumulating Subtotals variable = variable + new_value; Syntax: variable = variable + new_value; Examples year_pay = year_pay + pay; balance = balance - debit; counter = counter + 1; counter += 1; * } same
13
Increment/Decrement ++increment --decrement Surprise again! num++num-- ++num--num unary operators take a single operand num++, num-- ++num, --num
14
Increment/Decrement k = k + 1k = k + 3 k += 1k += 3 k ++ no equivalent
15
Increment/Decrement num = num + 1 num++ i = i + 1 i++ num = num - 1 num-- i = i - 1 i-- * * * * num += 1 num - =1 i += 1 i - = 1
16
Increment/Decrement k g value after execution k g 1. k = 7; 2. g = 2; 3. k = g; 4. g = g + 1; 7%#@$ 7 2 2 2 3 * * * * or combine 3 & 4 k = g++ Use it first, then add 1
17
postfix:first use it, then alter value Increment/Decrement postfix:first use it, then alter value z = 10; v = z--; cout <<v<<‘\t’<<z; v z 109 count = 10; k = count++; cout<<k<<‘\t’<<count; k count 1011 * *
18
output 1 cout << cnt++<<'\n'; 2 cout<<cnt<<'\n'; 3 cout<<(cnt++==guess)<<'\n'; 4 cout<<cnt<<'\n'; 5 cout<<cnt++<<'\n'; 6 cout<<cnt<< '\n'<<'\n'; Use Before Increment/Decrement * * * // print then inc 10// print then inc 11 // check then inc 1// check then inc 12 13 int cnt = 10, guess = 11;
19
output 1 cout << ++cnt<<'\n'; 2 cout<<cnt<<'\n'; 3 cout<<(++cnt==guess)<<'\n'; 4 cout<<cnt<<'\n'; 5 cout<<++cnt<<'\n'; 6 cout<<cnt<< '\n'<<'\n'; Use After Increment/Decrement * * * // inc then print 11// inc then print 11 // inc then check 0// inc then check 12 13 int cnt = 10, guess = 11;
20
Increment/Decrement a) cout << j++ b) cout << ++j c) cout << j += 14 d) cout << j /= 10 e) cout << j *= 10 f) cout << j - = 6 g) cout << (j = 5) + j h) cout << (j == 5) + j * * int j = 5; a) a) 5 b) b) 6 c) c) 19 d) d) 0 e) e) 50 f) f) -1 g) g) 10 h) h) 6
21
Math Library Functions cmath sqrt(n) fabs(n) cos(n) log10(n) log(n) pow(b, n) etc. * * * #include Function prototypes (or declarations)
22
Math Library Functions name of the function what it does data type of argument data type of returned value The actual function (object code) in usr/lib/libm.h g++ calculator.cc -lm Do not have to use
23
Math Library Functions function_name (argument); Syntax: function_name (argument); Ex.sqrt(49) pow(2.1, 3) abs(-34.5) cos(30) abs(34.5) *
24
Math Library Functions nested functions sqrt( pow ( fabs (-4), 3) ) = sqrt( pow ( 4.0, 3) ) = sqrt( 64.0 ) = 8.0 * * * You can use returned values from functions in any expression cout << sqrt(64.0); value = 23 * sqrt(number) + 5;
25
Type Casting The explicit conversion of a value from one data type to another. data_type (expression) Syntax: data_type (expression) * int (5.34 * 1.68) int (8.9712) This returns a value of 8.
26
Type Casting someInt = someDouble - 8.2; someInt = int(someDouble - 8.2); These are identical statements. *
27
Type Coercion The implicit (automatic) conversion of a value from one data type to another. someDouble = 42; is stored as 42.0 someInt = 11.9; is stored as 11 * g++ warning
28
Common Programming Errors not declaring all variables storing data of one type in a variable of a different type. The variable data type is kept. using a variable before assigning it a value mixing data types in an operation in integer division 4/5 = 0
29
More Common Programming Errors << ; forgetting << and ; not initializing variables before use ++ – applying ++ or – incorrectly
30
Practice Assignment An aquarium consists of four panels, all of which are metal except for the largest which is glass. All three side panels are rectangles. The bottom panel is a right triangle. There is no top panel. After getting user input on the dimension, calculate the area of the glass panel.
31
Assume the user enters positive real numbers expressed in inches. Comments are not required. sample output Height: [10.0] Width: [9.0] Length: [12] The glass is 150.0 square inches. Practice Assignment
32
dummy box for extra sound “Sleeping is not a waste of time.” “Sleeping is not a waste of time.” Deepak Chopra “Except in C++ class” “Except in C++ class” Joseph DeLibero
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.