Chapter 3.1 & 3.2

29
Chapter 3.1 & 3.2 Programming Assignment Statements Incrementing & Decrementing Math Library Functions

description

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. - PowerPoint PPT Presentation

Transcript of Chapter 3.1 & 3.2

Page 1: Chapter 3.1 & 3.2

Chapter 3.1 & 3.2

Programming Assignment Statements

Incrementing & Decrementing

Math Library Functions

Page 2: Chapter 3.1 & 3.2

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

*

Page 3: Chapter 3.1 & 3.2

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

Page 4: Chapter 3.1 & 3.2

Example 1: Output

Output:

The number stored in sum is 25

The number now stored in sum is 35

No surprises

Page 5: Chapter 3.1 & 3.2

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;

Page 6: Chapter 3.1 & 3.2

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

Page 7: Chapter 3.1 & 3.2

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

Page 8: Chapter 3.1 & 3.2

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

Page 9: Chapter 3.1 & 3.2

+= 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

Page 10: Chapter 3.1 & 3.2

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

* *

Page 11: Chapter 3.1 & 3.2

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

Page 12: Chapter 3.1 & 3.2

Increment/Decrement

++ increment

-- decrement

Surprise again!

unary operators

take a single operand

num++num++, num--num--

++num++num, --num--num

Page 13: Chapter 3.1 & 3.2

Increment/Decrement

k = k + 1 k = k + 3

k += 1 k += 3

k ++ no equivalent

Page 14: Chapter 3.1 & 3.2

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

Page 15: Chapter 3.1 & 3.2

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

Page 16: Chapter 3.1 & 3.2

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

* * * *

Page 17: Chapter 3.1 & 3.2

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;

Page 18: Chapter 3.1 & 3.2

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;

Page 19: Chapter 3.1 & 3.2

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

Page 20: Chapter 3.1 & 3.2

Math Library Functions

cmathsqrt(n)fabs(n)cos(n)log10(n)log(n)pow(b, n)etc.

* * *

#include <cmath>

Function prototypes

(or declarations)

Page 21: Chapter 3.1 & 3.2

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

Page 22: Chapter 3.1 & 3.2

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)

*

Page 23: Chapter 3.1 & 3.2

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;

Page 24: Chapter 3.1 & 3.2

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.

Page 25: Chapter 3.1 & 3.2

Type Casting

someInt = someDouble - 8.2;

someInt = int(someDouble - 8.2);

These are identical statements.These are identical statements.

* *

Page 26: Chapter 3.1 & 3.2

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

Page 27: Chapter 3.1 & 3.2

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

Page 28: Chapter 3.1 & 3.2

MoreCommon Programming Errors

forgetting <<<< and ;;

not initializing variables before use

applying ++++ or –– incorrectly

Page 29: Chapter 3.1 & 3.2

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