arithmetic expressions in c++. csce 1062 outline data declaration {section 2.3} arithmetic operators...

15
Arithmetic Expressions in C++

Upload: doris-hoover

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

Arithmetic Expressions in C++

Page 2: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 2

Outline Data declaration {section 2.3} Arithmetic operators in C++ {section

2.6} Mixed data type arithmetic in C++ Arithmetic expressions in C++ Operators’ precedence Arithmetic expressions’ evaluation

examples

Page 3: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 3

Data Declaration Remember the memory anatomy? It is much easier to set aside memory with

names (identifiers). Thus the declaration statements are used to set

aside memory with a specific name for the data and define its values and operations.

type identifier-list; Examples:

char response;char c, grade = ‘A’; int minElement;int n, j = 1;float score;float x, y, z = 40.0;bool flag;

The value can change during execution

-27.2

354

H

75.62

Address Contents

0

1024

1

.

.

.

2

.

.

.

ADD 73

x

n

c

Page 4: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 4

Data Declaration (cont’d)

When variables are declared

1. Memory allocated for value of specified type

2. Variable name associated with that memory location

3. Memory initialized with values provided (if any)

27

Page 5: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 5

Constant Declarations A constant is a memory cell whose value cannot

change during execution once it is set in the declartion.

const type constant-identifier = value; E.g.:

const float KM_PER_MILE = 1.609; It is a good C++ programming practice to

capitalize all letters for constant names. Any data type (e.g. int, float, char, or bool)

could be declared as constant.

Page 6: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 6

Identifiers A valid identifier (variable or function name) must

consist of letters, digits, or underscore only. A valid identifier must not begin with a digit. Valid identifiers: letter, letter1, _letter Invalid identifiers: 1letter, float, hell o You cannot use a C++ reserved word as an identifier. Always try to associate meaningful identifiers. It is a good C++ programming practice to capitalize the

first letter of each word (after the first) when using multiple word identifiers

Remember that C++ is case sensitive (cost != Cost)

Page 7: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 7

Arithmetic Operators in C+++ Addition- Subtraction* Multiplication/ Division% Modulus (remainder operator, only for integers)

Examples of integer modulus:7 % 2 = 1299 % 100 = 9949 % 5 = 415 % 0 undefined

Examples of integer division (result is integer):15 / 3 = 515 / 2 = 70 / 15 = 015 / 0 undefined

Page 8: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 8

Mixed Data Type Arithmetic in C++

Example:

4.6 / 2 evaluates to 2.3

Rule: when an integer and a floating point operand are combined by an operator, the integer gets converted to the floating point type.

Page 9: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 9

Arithmetic Expressions in C++An assignment statement has the following format:

variable = expression;

e.g.:

kms = KM_PER_MILE * miles; The arithmetic expression to the right of the

assignment operator (=) gets evaluated first Then the result is stored in the variable on the

left side of the assignment operator

Page 10: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 10

Arithmetic Expressions in C++ (cont’d)

Mixed-type assignments:If the variable on left side of assignment is of different type than

the type of the evaluated expression on the right side of =, the result of the expression must be converted to the appropriate type.

Examples:float a, b, x;int m, n;

a = 10; // result is 10.0 stored in ab = 3.5;m = 5;n = 10;x = m / n; // result is 0 assigned to x; therefore x is 0.0m = b * 3; // result is 10 assigned to m

Page 11: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 11

Arithmetic Expressions in C++ (cont’d)

What if the arithmetic expression contains more than one operator?

x = z - (a + b / 2) + w * -y; How many operators do we have in the above expression? Computers follow operator precedence rules to evaluate

expressions. The formula: m = y - b

x - a The computer equivalent is:

m = (y - b) / (x - a);

Must use ( ) because of operator precedence.

Page 12: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 12

Order of Operator Precedence ( ) nested expressions

evaluated inside outunary +, -*, /, %

If there are several, then evaluate from left-to-rightbinary +, -

If there are several, then evaluate from left-to-right =

Highest

Lowest

Associativity

Warning: watch out for the types of operands and the type of the result from evaluating each operation!

Page 13: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 13

Example 1x = z - (a + b / 2) + w * -y; x integer z

a b

w y

3

2-

5

-

7

1

11

8 - (3 + 9 / 2) + 2 * --5 9

8

-5/

4+

x = 8 - (3 + 9 / 2) + 2 * - -5 9 / 2

(3 + 4 ) 8 - 7 + 2 * - - 5 8 - 7 + 2 * 5 8 - 7 + 10 1 + 10

11

* 10

+

Page 14: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 14

Example 2m = x + k / 2; m integer

x

k

5.5

5/2

convert to float

+ 2.0

7.5

convert to int

7

5.5 + 5 / 2

Page 15: Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in

CSCE 106 15

Next lecture will be about

Library Functions