Chapter 3 - YSU CSIS

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
riker = 5.6
invalid
5 = riker
*
Assignment Example 1
#include <iostream>
sum
Using namespace.std;
25 35
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’;
}
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
Sum
cout
???
int sum;
0
sum = 0; // initialize sum
cout << "\nThe value of sum is
0
initially set to " << sum;
10
sum = sum + 10;
10
cout << "\nsum is now " << sum;
30
sum = sum + 20;
cout << "\nsum is now " << sum;
30
sum = sum - 30;
0
cout << "\nsum is now " << sum;
0
sum = sum - 40;
cout << "\nThe final sum is " << -40
sum;
-40
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
+= -= *= /= %=
1.
2.
3.
4.
5.
6.
i += 2
r *= 7
j *= (k + 3)
x /= y - 4
hour %= 12
left -= t_out
i=i+2
r = r *7
j = j * (k + 3)
x = x /y - 4
hour = hour % 12
left = left - t_out
**
Common Use
Accumulating Subtotals
Syntax:
variable = variable + new_value;
Examples
year_pay = year_pay + pay;
balance = balance - debit;
counter = counter + 1;
same
counter += 1;
}
*
Increment/Decrement
++ increment
-- decrement
Surprise again!
unary operators
take a single operand
num++, num-++num, --num
Increment/Decrement
k=k+1
k=k+3
k += 1
k += 3
k ++
no equivalent
Increment/Decrement
num = num + 1
num++
num = num - 1
num--
i=i+1
i++
i=i-1
i--
num += 1
i += 1
num -=1
i -= 1
***
**
Increment/Decrement
1.
2.
3.
4.
k = 7;
g = 2;
k = g;
g = g + 1;
or combine 3 & 4
k = g++
value after execution
k
g
7
%#@$
7
2
2
2
2
3
Use it first,
then add 1
***
**
Increment/Decrement
postfix:
first 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
****
Use Before Increment/Decrement
int cnt = 10, guess = 11;
1
2
3
4
5
6
output
10
cout << cnt++<<'\n';
11
cout<<cnt<<'\n';
cout<<(cnt++==guess)<<'\n'; 1
12
cout<<cnt<<'\n';
12
cout<<cnt++<<'\n';
13
cout<<cnt<< '\n'<<'\n';
// print then inc
// check then inc
***
***
Use After Increment/Decrement
int cnt = 10, guess = 11;
1
2
3
4
5
6
output
11
cout << ++cnt<<'\n';
11
cout<<cnt<<'\n';
cout<<(++cnt==guess)<<'\n'; 0
12
cout<<cnt<<'\n';
13
cout<<++cnt<<'\n';
13
cout<<cnt<< '\n'<<'\n';
// inc then print
// inc then check
***
***
int j = 5;
Increment/Decrement
a) cout << j++
b)
c)
d)
e)
f)
g)
h)
cout << ++j
cout << j += 14
cout << j /= 10
cout << j *= 10
cout << j -= 6
cout << (j = 5) + j
cout << (j == 5) + j
a)
b)
c)
d)
e)
f)
g)
h)
5
6
19
0
50
-1
10
6
****
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
Syntax:
function_name (argument);
Ex. sqrt(49)
abs(-34.5)
abs(34.5)
pow(2.1, 3)
cos(30)
*
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)
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.0
someInt = 11.9;
11
is stored as
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
in
data types in an operation
integer division 4/5 = 0
More
Common Programming Errors
forgetting
not
<< and ;
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