1 operators and expressions. 2 operators arithmetic operators relational and logical operators...

34
1 Operators And Expressions

Post on 21-Dec-2015

323 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

1

OperatorsAnd

Expressions

Page 2: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

2

Operators

• Arithmetic Operators

• Relational and Logical Operators

• Special Operators

Page 3: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

3

Arithmetic Operators

Operator Action– Subtraction, also unary minus+ Addition* Multiplication/ Division% Modulus-- Decrement++ Increment

Page 4: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

4

Precedence and Associativity• Precedence of the Arithmetic operators:

High ++ --

- (unary minus)

* / %

Low + -

- a * b – c ((- a) * b) – c

Page 5: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

5

• Expressions inside parenthesesinside parentheses are evaluated first.

1 * (2 - 3)

• Operators on the same level of precedence are evaluated from left to right.

(Associativity).

1 + 2 + 3 + 4 –5

(((1 + 2) + 3) + 4) –5

Page 6: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

6

Increment & Decrement Operators• Provide a concise notation for incrementing or

decrementing a variable by 1.

• Are unary operators.

++x or x++ --x or x--

• Can be applied to variables but not to constants or ordinary expressions.

++i; legalcnt--; legal777++; illegal++(a * b -1); illegal

Page 7: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

7

• May either prefix or postfix the operand.

Prefix ++x; or Postfix x++;

x = x + 1;

++ & -- both cause a value of a variable to change in memory.

( Have a side effect).

Page 8: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

8

Increment Postfix: i++;

Expression value is the current value (Before you increment) then it increments.

“use - then increment”

Increment Prefix: ++i;

Expression value is the value After you increment.

“increment - then use”

Decrement Postfix: i--;

“use - then decrement”

Decrement Prefix: --i;

“decrement - then use”

Page 9: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

9

Examples

x =10; y = ++x; y 11x =10; y = x++; y 10

int i = 3, j = 2, k;

i++; ij = ++i; j i k = i++; k i k = (--j+3) k j

4

5 55 6

47

Page 10: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

10

l = 4;

n = 3;

m = 2;

x = l * n + m++; x

After the assignment to x.

m

14

3

Page 11: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

11

int a, b, c=0;

a = ++c;

b = c++;

a = ? b = ? c= ?

int b = 2, d = 4;

7--b*++d 7-((-b)*(++d)) ?

int j = 2, k = 3, m = 4;

j*=k=m+5 j=(j*(k=(m+5))) ?

Page 12: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

12

int a,b;

a = 1;

b = 12;

printf (“a+++b = %d/n”, a+++b);

a = 1;

b = 12;

printf (“a++ +b = %d/n”, a++ +b);

a = 1;

b = 12;

printf (“a+ ++b =% d/n”, a+ ++b);

Page 13: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

13

Relational and Logical Operators

• Relational refer to the relationship that value can have with one another.

• Logical refers to the ways these relationships can be connected.

• True is any value other than zero. False is zero.

Page 14: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

14

Relational Operators:Operator Action > Greater than >= Greater than or equal < Less than <= Less than or equal = = Equal != Not equal    

                Logical Operators:

Operator Action && AND || OR ! NOT

Page 15: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

15

The truth table for the logical operators. True(1), False(0).

p q p&&q p || q !p0 0 0 0 10 1 0 1 11 1 1 1 01 0 0 1 0

Page 16: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

16

Precedence and Associativity

High !> >= < <== = !=&&

Low ||

!0&&0||0 ((!0)&&0)||0 FALSE

Page 17: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

17

• Both are lower in precedence than the arithmetic operators.

10 > 1 + 12 10 > (1 + 12) FALSE 0

Associativity left to right.

int x;x = 100;printf(''%d", x>10); __?

Page 18: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

18

!A is false (0) if A’s value is: __.

is true (1) if A’s value is: __.

!!5 ! (!5) ! (0) 1

NOT (Any NON-zero) 0

5 && 3 ?

Examples

Page 19: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

19

int i, j = 1;

j = j && (i = 2);

1) (i=2) i

2) && j && 2

true && true 1

3) = j1

1

2

( ) needed

Page 20: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

20

j i

j = j && (i = = 3);

1) (i = = 3) false 0

2) && j && 0 0

3) = j

( ) not needed

1

21

0

Page 21: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

21

j i

j = j || (i/2)

1) (i/2) (2/2) 1

2) || j || 1 true1

3) = j

0 2

0

1

( ) not needed

Page 22: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

22

j i

j = !j && (i = i + 1);

1) i + 1 3

2) = i

3) ! !j !1 0

4) && 0 && 3

5) = j

3

21

0

Page 23: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

23

The Comma OperatorLowest precedence of all the operators.Causes a sequence of operations, “do this

and this and this…”.Is a binary operator.

expression_1, expression_2Associates left to right.

expression_1 is evaluated firstexpression_2 is evaluated second

x = (y=3, y+1); x 4 ( ) needed

Page 24: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

24

The Comma Expression as a whole has the value and type of expression_2.

int i = 2;

j = 4;

j = i++, i - j;

* i

* j (3-4)

3

-1

Page 25: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

25

• It allows multiple initializations and multiple processing of indices.

for (sum=0, i=1; i<=n; ++i)sum += i;

Comma Operators can be useful in control statementsthough many “C” advocates

discourage their use.

Page 26: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

26

e.g. int i;i = 0; for ( ; i < 10; putchar (‘a’ + i), i++);

will output?3rd expression in the for statement is a comma

expression.

putchar is called ,executed.Then i is increased.

Most commas in a program DO NOT represent comma operators.

see text - pg. 99

Page 27: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

27

The ( ) and [ ] Operators

• Parentheses are operators that increase the precedence of the operations inside them.

• Square brackets perform array indexing (See Chapter 9 for a discussion of array.)

int main(void){ char s[80];   s[3] = 'X';  printf(''%c", s[3]);  return 0;}

Page 28: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

28

The Conditional Operator ?

• Ternary operator.• A powerful and convenient operator that

replaces certain statements of the if-then-else form.

Exp1 ? Exp2: Exp3

Stands for: if Exp1then Exp2else Exp3

Page 29: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

29

x = 10;y = x>9 ? 100 : 200;

x = 10;

if(x>9) y = 100;

else y = 200;

Examples

Page 30: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

30

int i, h, j = 2;

i = (j==2) ? 1:3;

k = (i>j) ? i:j;

( ) not needed

i get 1

k get max of I or j

Page 31: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

31

This statement does what?

c = (c > =‘a’ && c < = ‘z’) ? c - (‘z’ - ‘Z’):c;

IF True - have a Lower case letter in the variable “C”.

Exper2: c - (‘z’ - ‘Z’) will give Capital

Letter of whatever is in C.

e.g. a - (‘z’ - ‘Z’) 97 - (122 – 90) = 65 which is ‘A’.

IF False – Capital Letter and leaves it alone.

Page 32: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

32

Expressions• An expression in C is any valid

combination of operators, constants, functions and variables.

• An statement is a valid expression followed by a semicolon.

func();   /* a function call */

a = b+c;  /* an assignment statement */

b+f();    /* a valid, but strange statement */

;         /* an NULL statement */

Page 33: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

33

Null Statement: ; [A semi-colon alone by itself].

Can be useful in loops & conditional statements.

The body of a loop could be empty

.

Scanf(“%d”, &x);

While(printf(“%d”,x) ,scanf(“%d”,&x)==1)

;

Page 34: 1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

34

• Spacing and Parentheses

• Redundant or additional parentheses do not cause errors or slow down the execution of an expression.

x=10/y-(127/x);x = 10 / y - (127/x);

x = y/3-34*temp+127;x = (y/3) - (34*temp) + 127;