290 likes | 423 Vues
This chapter covers the fundamentals of assignment statements, incrementing, and decrementing in C++. It explains the assignment operator, which assigns the value on the right to the variable on the left. Syntax examples demonstrate how variables are initialized and manipulated through assignment. Additionally, shorthand notations for assignment operators, including addition, subtraction, multiplication, division, and modulus, are introduced. The chapter also covers the use of increment (`++`) and decrement (`--`) operators, their syntax, and practical examples to illustrate their behavior.
E N D
Chapter 3.1 & 3.2 • Programming • Assignment Statements • Incrementing & Decrementing • Math Library Functions
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 • valid invalid riker = 5.6 5 = riker *
Assignment Example 1 sum • #include <iostream> • Using namespace.std; • void main(void) • { • int sum; • sum = 25; //initialize sum • cout << “The number stored in sum is " • << sum; • sum = sum + 10; • cout << "\nThe number now stored in sum is " << sum<< ‘\n’; • } 25 35
Example 1: Output • Output: • The number stored in sum is 25 • The number now stored in sum is 35 • No surprises
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;
A Trace of Ex2 Assignment Example 2 Sumcout ??? 0 0 10 10 30 30 0 0 -40 -40 • 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;
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
Assignment Operators Surprise! • 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) MyVariable = MyVariable + TaxRate * Cost
Assignment Operators • += 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+= -= *= /= %= Assignment Operators • 1. i += 2 i = i + 22. r *= 7 r = r *73. j *= (k + 3) j = j * (k + 3) • 4. x /= y - 4 x = x /y - 45. hour %= 12 hour = hour % 126. left -= t_out left = left - t_out * *
Common UseAccumulating Subtotals Syntax:variable = variable + new_value; • Examples year_pay = year_pay + pay; balance = balance - debit; counter = counter + 1; counter += 1; }same *
Increment/Decrement • ++ increment-- decrementSurprise again! • unary operators take a single operandnum++, num--++num, --num
Increment/Decrement • k = k + 1 k = k + 3 • k += 1 k += 3 • k ++ no equivalent
Increment/Decrement num = num - 1num-- i = i - 1i-- • num = num + 1num++ • i = i + 1i++ num += 1 num -=1 i += 1 i -= 1 * * * * *
Increment/Decrement • value after executionk g1. k = 7;2. g = 2;3. k = g;4. g = g + 1; 7 %#@$ 7 2 2 2 2 3 or combine 3 & 4 k = g++ Use it first, then add 1 * * * * *
Increment/Decrementpostfix: first use it, then alter value count = 10;k = count++;cout<<k<<‘\t’<<count; k count 10 11 • z = 10;v = z--;cout <<v<<‘\t’<<z; • v z 10 9 * * * *
Use Before Increment/Decrement • output • 1cout << cnt++<<'\n'; • 2 cout<<cnt<<'\n'; • 3 cout<<(cnt++==guess)<<'\n'; • 4 cout<<cnt<<'\n'; • 5 cout<<cnt++<<'\n'; • 6 cout<<cnt<< '\n'<<'\n'; int cnt = 10, guess = 11; 10// print then inc 11 1// check then inc 12 12 13 * * * * * *
Use After Increment/Decrement • 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'; int cnt = 10, guess = 11; 11// inc then print 11 0// inc then check 12 13 13 * * * * * *
int j = 5; Increment/Decrement a) 5 b) 6 c) 19 d) 0 e) 50 f) -1 g) 10 h) 6 • 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 * * * * * * * *
Math Library Functions • cmath • sqrt(n) fabs(n) cos(n) • log10(n) log(n) • pow(b, n) etc. #include <cmath> Function prototypes (or declarations) * * *
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
Math Library Functions • Ex. sqrt(49) pow(2.1, 3) • abs(-34.5) cos(30) • abs(34.5) Syntax:function_name (argument); *
Math Library Functions • sqrt( pow( fabs (-4), 3) ) = • sqrt( pow( 4.0 , 3) ) = • sqrt( 64.0 ) = 8.0 nested functions You can use returned values from functions in any expression cout << sqrt(64.0); value = 23 * sqrt(number) + 5; * * *
Type Casting • The explicit conversion of a value from one data type to another. Syntax:data_type (expression) int (5.34 * 1.68) int (8.9712) This returns a value of 8. * *
Type Casting • someInt = someDouble - 8.2; • someInt = int(someDouble - 8.2); • These are identical statements. * *
Type Coercion • The implicit (automatic) conversion of a value from one data type to another. someDouble = 42; is stored as 42.0someInt = 11.9; is stored as 11 g++ warning *
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
MoreCommon Programming Errors • forgetting << and; • not initializing variables before use • applying ++ or – incorrectly
dummy box for extra sound “Sleeping is not a waste of time.” Deepak Chopra “Except in C++ class” Joseph DeLibero