chapter 3.1 & 3.2
Post on 14-Jan-2016
58 Views
Preview:
DESCRIPTION
TRANSCRIPT
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#include <iostream>#include <iostream>Using namespace.std;Using namespace.std;void main(void)void main(void){{ int sum;int sum; sum = 25;sum = 25; //initialize sum//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 " cout << "\nThe number now stored in sum is "
<< sum<< ‘\n’; << sum<< ‘\n’;}
sum
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 2int sum;int sum;
sum = 0;sum = 0;cout << "\nThe value of sum is cout << "\nThe value of sum is initially set to " << sum;initially set to " << sum;
sum = sum + 10;sum = sum + 10;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 20;sum = sum + 20;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum - 30;sum = sum - 30;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum - 40;sum = sum - 40;cout << "\nThe final sum is " << sum;cout << "\nThe final sum is " << sum;
Assignment Example 2int sum;int sum;
sum = 0;sum = 0; // initialize sum// initialize sumcout << "\nThe value of sum is cout << "\nThe value of sum is initially set to " << sum;initially set to " << sum;
sum = sum + 10;sum = sum + 10;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 20;sum = sum + 20;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum - 30;sum = sum - 30;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum - 40;sum = sum - 40;cout << "\nThe final sum is " << sum;cout << "\nThe final sum is " << sum;
Sum cout
???
0
0
10
10
30
30
0
0
-40
-40
A Trace of Ex2
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 -40Hopefully, no surprises here either
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
+= add then assign-= subtract then assign*= multiply then assign/= divide then assign%= modulus, then assign
X -= 3 X = X - 3pay *= 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
Assignment Operators
* *
Common UseAccumulating Subtotals
Syntax: variable = variable + variable = variable + new_value;new_value;
Examplesyear_pay = year_pay + pay;balance = balance - debit;counter = counter + 1;counter += 1;
*
} same
Increment/Decrement
++ increment
-- decrement
Surprise again!
unary operators
take a single operand
num++num++, num--num--
++num++num, --num--num
Increment/Decrement
k = k + 1 k = k + 3
k += 1 k += 3
k ++ no equivalent
Increment/Decrement
num = num + 1num = num + 1
num++num++
i = i + 1i = i + 1
i++i++
num = num - 1num = num - 1
num--num--
i = i - 1i = i - 1
i--i--
* * ** *
num += 1num += 1 num num --=1=1
i += 1 i += 1 i i --= 1= 1
Increment/Decrementvalue after execution
kk g g
1. k = 7;
2. g = 2;
3. k = g;
4. g = g + 1;
7 %#@$
7 2
2 2
2 3
* * ** *
or combineor combine 3 & 4 3 & 4
k = g++k = g++Use it first, then add 1
Increment/Decrement
postfix:postfix: first use it, then alter valuefirst use it, then alter value
z = 10;
v = z--;
cout <<v<<‘\t’<<z;
v z
10 9
count = 10;
k = count++;
cout<<k<<‘\t’<<count;
k count
10 11
* * * *
outputoutput
11 cout << cnt++<<'\n';cout << cnt++<<'\n';
22 cout<<cnt<<'\n';cout<<cnt<<'\n';
33 cout<<(cnt++==guess)<<'\n';cout<<(cnt++==guess)<<'\n';
44 cout<<cnt<<'\n';cout<<cnt<<'\n';
55 cout<<cnt++<<'\n';cout<<cnt++<<'\n';
66 cout<<cnt<< '\n'<<'\n';cout<<cnt<< '\n'<<'\n';
Use Before Increment/Decrement
* * ** * *
10 // print then inc// print then inc
11
1 // check then inc// check then inc
12
12
13
int cnt = 10, guess = 11;int cnt = 10, guess = 11;
outputoutput
11 cout << ++cnt<<'\n';cout << ++cnt<<'\n';
22 cout<<cnt<<'\n';cout<<cnt<<'\n';
33 cout<<(++cnt==guess)<<'\n';cout<<(++cnt==guess)<<'\n';
44 cout<<cnt<<'\n';cout<<cnt<<'\n';
55 cout<<++cnt<<'\n';cout<<++cnt<<'\n';
66 cout<<cnt<< '\n'<<'\n';cout<<cnt<< '\n'<<'\n';
Use After Increment/Decrement
* * ** * *
11 // inc then print// inc then print
11
0 // inc then check// inc then check
12
13
13
int cnt = 10, guess = 11;int cnt = 10, guess = 11;
Increment/Decrementa) 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;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
Math Library Functions
cmathsqrt(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
Syntax: function_name (argument);function_name (argument);
Ex. sqrt(49) pow(2.1, 3)
abs(-34.5) cos(30)
abs(34.5)
*
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;
Type Casting
The explicit conversion of a value from one data type to another.
Syntax: data_type (expression)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.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.0
someInt = 11.9; is stored as 11
*
g++ warning
Common Programming ErrorsCommon 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 Sleeping is notnot a waste a waste of time.”of time.”
Deepak Chopra
““Except in Except in C++C++ class” class”Joseph DeLibero
top related