36
Slide 7- 1

Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Embed Size (px)

Citation preview

Page 1: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Slide 7- 1

Page 2: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Statements, Operators & Operands

Arithmetic Operators

Relational Operators

Logical Operators

Assignment Operators

Copyright © 2007 Pearson Education, Inc. Publishing as

Pearson Addison-Wesley

Page 3: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Elements of a program

Literals fixed data written into a program

Variables & constants placeholders (in memory)

for pieces of data

Types sets of possible values for data

Expressions combinations of operands (such as

variables or even "smaller" expressions) and

operators. They compute new values from old ones.

Assignments used to store values into variables

Statements "instructions". In C, any expression

followed by a semicolon is a statement

Page 4: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Elements of a program (cont…)

Control-flow constructs constructs that allow

statements or groups of statements to be executed

only when certain conditions hold or to be executed

more than once.

Functions named blocks of statements that

perform a well-defined operation.

Libraries collections of functions.

Page 5: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Statement

Statements are elements in a program which

(usually) ended up with semi-colon (;)

e.g. below is a variables declaration statement

int a, b, c;

• Preprocessor directives (i.e. #include and define)

are not statements. They don’t use semi-colon

Page 6: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Some types of statement in C++

Page 7: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

An expression statement is a statement that results a value

SOME EXAMPLES OF EXPRESSION VALUE

• Literal expression

e.g. 2, “A+”, ‘B’ The literal itself

• Variable expression

e.g. Variable1

• Arithmetic/Mathematic

expression

e.g. 2 + 3 -1

The content of the variable

The result of the operation

Page 8: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Operators & Operands Operands is something that make the operator be act

Operators can be classified according to

the type of their operands and of their output

Arithmetic

Relational

Logical

Bitwise

the number of their operands

Unary (one operand)

Binary (two operands)

Ternary (unique to the C language)

A combination of operands and operators called as

Operations!

Page 9: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Arithmetic operators

They operate on numbers and the result is a

number.

The type of the result depends on the types of the

operands.

If the types of the operands differ (e.g. an integer

added to a floating point number), one is

"promoted" to other.

The "smaller" type is promoted to the "larger" one.

char int float double

Page 10: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Example of promotion:

The result of the following “double division” is 2.5

5 / 2.0

Before the division process, 5 is promoted from integer 5 to

float 5.0

The result of the following “integer division” is 2

5 / 2

There is no promotion occurred. Both operands are the same

type.

Page 11: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Arithmetic operators: +, *

+ is the addition operator

* is the multiplication operator

They are both binary

Arithmetic operator:

This operator has two meanings:

subtraction operator (binary)

negation operator (unary)

e.g. 31 - 2

e.g. -10

Page 12: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Arithmetic operator: %

The modulus (remainder) operator.

It computes the remainder after the first operand is divided by

the second

It is useful for making cycles of numbers:

For an int variable x : if x is: 0 1 2 3 4 5 6 7 8 9 ... (x%4) is: 0 1 2 3 0 1 2 3 0 1 ...

e.g. 5 % 2 is 1, 6 % 2 is 0

Arithmetic operator: /

Division operator

CAREFUL! The result of integer division is an integer:

e.g. 5 / 2 is 2, not 2.5

Page 13: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %
Page 14: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %
Page 15: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Relational operators

These perform comparisons and the result is what is

called a boolean: a value TRUE or FALSE

FALSE is represented by 0; anything else is TRUE

The relational operators are:

< (less than)

<= (less than or equal to)

> (greater than)

>= (greater than or equal to)

== (equal to)

!= (not equal to)

Page 16: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %
Page 17: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Logical operators (also called Boolean operators)

These have Boolean operands and the result is also a

Boolean.

The basic Boolean operators are:

&& (logical AND)

|| (logical OR)

! (logical NOT) -- unary

Page 18: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %
Page 19: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Binary expression

Page 20: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Unary Expression

Page 21: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Ternary Expression

(a>2) ? 1: 0

Operator

First operand

is a condition

Second operand

is a value Third operand

is another value

Page 22: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Assignment operator(=)

Binary operator used to assign a value to a variable.

Page 23: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Remember, an expression results a value. So, what is the result of an assignment expression?

The value of an assignment expression is its right operand

e.g.

int a=10;

cout << a=7;

Output: 7 (not 10).

Because the value of expression a=7 is 7 (i.e its right operand)

Assignment is a special expression. Beside results a value, it also does other thing which is putting the value into its left

operand. This is called a side effect.

In the previous example, a side effect has occurred to variable a after evaluating the assignment expression (a=7). Now, the variable has a new value which is 7.

Page 24: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Combined Assignment Operators

Page 25: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Special assignment operators

write a += b; instead of a = a + b;

write a -= b; instead of a = a - b;

write a *= b; instead of a = a * b;

write a /= b; instead of a = a / b;

write a %= b; instead of a = a % b;

Page 26: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Special assignment operators Increment, decrement operators: ++, --

Instead of a = a + 1 you can write a++ or ++a

Instead of a = a - 1 you can write a-- or --a

These operators cause side effect

What is the difference?

num = 10;

ans = num++;

num = 10;

ans = ++num;

First increment num,

then assign num to ans.

In the end,

num is 11

ans is 11

First assign num to ans,

then increment num.

In the end,

num is 11

ans is 10

post-increment pre-increment

Page 27: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Result of pre-fix Increment

Page 28: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Result of Post-fix Increment

Page 29: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Precedence & associativity

How would you evaluate the expression

17 - 8 * 2 ?

Is it 17 - (8 * 2)

or (17 - 8) * 2 ?

These two forms give different results.

We need rules!

Page 30: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Precedence & associativity

When two operators compete for the same operand

(e.g. in 17 - 8 * 2 the operators - and * compete for

8) the rules of precedence specify which operator

wins.

The operator with the higher precedence wins

If both competing operators have the same

precedence, then the rules of associativity

determine the winner.

Page 31: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Precedence & associativity

! Unary –

* / %

+ –

< <= >= >

= = !=

&&

||

=

higher precedence

lower precedence

Associativity: execute left-to-

right (except for = and unary – )

Page 32: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Example: Left

associativity

3 * 8 / 4 % 4 * 5

Page 33: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Example: Right associativity

a += b *= c-=5

Page 34: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Precedence & associativity

Examples:

X =17 - 2 * 8 Ans: X=17-(2*8) , X=1

Y = 17 - 2 - 8 Ans: Y = (17-2)-8, Y=7

Z = 10 + 9 * ((8 + 7) % 6) + 5 * 4 % 3 *2 + 1 ?

Not sure? Confused? then use parentheses in your code!

Page 35: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Algebraic Expressions

Multiplication requires an operator: Area=lw is written as Area = l * w;

There is no exponentiation operator: Area=s2 is written as Area = pow(s, 2);

Parentheses may be needed to maintain order of operations:

is written as

m = (y2-y1) /(x2-x1); 12

12

xx

yym

Page 36: Slide 7- 1zmnizam.weebly.com/.../week5_chap4_-arithmetic_expresssion.pdf · Elements of a program ... negation operator (unary) e.g. 31 - 2 e.g.-10 . Arithmetic operator: %

Algebraic Expressions (cont…)