chapter 3 dti2143

Post on 17-May-2015

642 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

EXPRESSIONS AND OPERATORS

CHAPTER 3

DTI 21431

2

CONTENTS

1. Arithmetic Operators

2. Assigning Mathematical Operator Combinations

3. Unary Sign Operators

4. Increments And Decrement Operator

5. Conditional Operator

6. Logical Expressions

7. Logical Operator

CHAPTER 4

2

INTRODUCTION

1. In any programming languages, we need operators to construct statements to do an operation and calculations.

2. In this chapter, you will be introduced to several operators such as:

a. Unary Operatorsb. Binary Operatorsc. Ternary Operators

3. Operator can be applied to variables or any elements in an expression to produce some kind of computation or action.

3

ARITHMETIC OPERATORS

1. We can form more complicated arithmetic expressions by using arithmetic operators.

2. There are two types of arithmetic operators in c such as:

a) Unary arithmetic operators• Required one operand.

b) Binary arithmetic operators• Required two operand.

4

1. Examples of Unary arithmetic operators

2. Binary arithmetic operators

Operators Symbol

1. Unary Plus +

2. Unary Minus -

3. Increment Operators ++

4. Decrement Operators --

Operators Symbol

1. Additional Operator +

2. Subtraction Operator -

3. Multiplication Operator *

4. Division Operator /

5. Modulus Operator (Remainder of division) % 5

Example 1#include <stdio.h>#include <conio.h>int main(void){int num= 20;int val=30;num=val+5; /* num=30+5*/val=num-val; /*val=35-30*/num=num+2; /*num=35-5*/val=30/7; /*val=30/7*/getch();return 0;} 6

Example 2#include <stdio.h>#include <conio.h>int main(void){int num= 20;printf (“%d\n\n”, num);int val=30;printf (“%d\n\n”, val);num=val+5; printf (“%d\n\n”, num);val=num-val;printf (“%d\n\n”, val);num=num+2;printf (“%d\n\n”, num);val=30/7;printf (“%d\n\n”, val);getch();return 0;}

Output

20

30

35

5

37

4

7

Example 3

#include <stdio.h>#include <conio.h>int main(void){int a,b,c,d,result;a=10;b=20;c=15;d=8;result=a*b/(-c*31%13)*d;printf ("%d",result);getch();return 0;}

Output

-160

8

How to Solves Example 3?

• The expression will evaluated 6 steps

a=10,b=20,c=15,d=8,e=40,a * b / (-c * 31 % 13 )* d

Step Operator Reduced Expression

1. Unary - a * b / (-15 * 31 % 13 ) * d

2. * a * b / (-465 % 13 ) * d

3. % a * b / (-10) * d

4. * 200 / (-10) * d

5. / -20 * d

6. * -1609

Example 3

#include <stdio.h>#include <conio.h>int main(void){int num =5;num -=3;num +=4;printf ("%d", num);getch();return 0;}

Output

6

10

Example 4

Operator Syntax Meaning

*= price *=2.5; price= price * 2.5;

/= total /=2;

%= month %=2; month=month%=2;

+= bonus +=800;

-= balance -=sum +tax;

total = total / 2;

bonus=bonus+800;

balance =balance- sum + tax

11

INCREMENTS AND DECREMENT OPERATOR

• In C, we have two special operators for incrementing or decrementing a variable by one.

• The operators are:i. Unary Increment operators : ++

a) ++i (prefix)

b) i++ (postfix)

ii. Unary Decrement Operators : --a) --i (prefix)

b) i-- (postfix)

12

a) Unary Increment operators : ++

» To increment a variable by 1» Also used as counter in loop structure» Eg: i=i+1; or» i +=1;

b) Unary Decrement Operators : --

» To decrement a variable by 1» Eg: i=i-1 or» i -=1;

13

INCREMENTS OPERATOR

++i (prefix) i++(postfix)

In the latter case, the operator comes just in front of the variable.

Eg:++grade;++mark;++a;

In the former case, we place the operator after the variable.

Eg:grade++;mark++;a++;

Incrementing “i” value. Use “i” value in expression.

Use new i value in expression

Eg:int i=0;++i; /* 1 */printf(“%d”, i);

Incrementing i value (before expression executes)

Eg:int i=0;i++; /* 0 + 1(in memory) */printf(“%d”, i);

/*Output : 1*/ /*Output : 1*/

14

LOGICAL EXPRESSIONS

• C provides logical operators that are used to combine expressions into logical expressions.

15

RELATIONAL AND EQUALITY OPERATOR

OPERATOR MEANING CONDITION VALUE

< Less Than Power<MAX_POW 0(false)

<= Less than or equal to

x<=0 1(true)

> Greater Than x>=y 0(false)

>= Greater than or equal to

item>MIN_ITEM 1(true)

== Equal to mom_or_dad==‘M’ 1(true)

!= Not equal to num !=SENTINEL 0(false)

16

Example 1

OPERATOR CONDITION VALUE

1. == (a+b)==c 0

2. != a!=b 1

3. < b<a

4. > a>c

5. <= (b+c)<=a

6. >= c>=b

a = 6;b = 1;c = - 2;

1

1

0

0

17

LOGICAL OPERATOR

• The purpose of logical operator is to combine and check the results of logical expressions.

OPERATOR MEANING

! NOT

&& AND

|| OR

18

EXAMPLE 1

EXPRESSION VALUE (TRUE /FALSE)

1. ( 5 = = 5 ) && ( 7 ! = 2 ) 1(TRUE)

2. ( 5 > 4 ) | | ( 9 < 3 ) 1(TRUE)

3. ( 2 > 2 ) && ( 7 = = 7 )

4. ! ( 7= = 6 )

5. ( 1 = = 2 ) | | ( 6 > = 9 )

0(FALSE)

1(TRUE)

0(FALSE)

19

EXAMPLE OF QUESTIONS

1) Given:

int val, x = 4, y = 3, z = 10;val =(x+y>=3*z) == (x!=3*z+y);

The value of val is: 

a) 1

b) 0

c) 10

d) 1720

ANSWER

B

2) Given:

int val, a = 1, b = 2, c = 3, d = 4;val = d+a%c*b;

The value of val is:

a. 6

b. 5

c. 0

d. 121

ANSWER

A

3)

int cost = 2;

int price = 5;

cost += ++price * 2;

 

What is answer for cost?

22

ANSWER

14

4)

Assume count = 21.

What is the current value of

count at each of these statements?

 

a) count-- =……….

b) ++count =……….

23

ANSWER

•21•21

5)

#include<stdio.h>#include<conio.h>int main(){ int n01=39,n02=100; printf("%d",++n01); printf("\n%d",n01--+2); printf("\n%d",(--n01)+(++n02)); printf("\n%d",n01); getch (); return 0; }

6)

#include<stdio.h>#include<conio.h>int main(){ int x=6,y=6; printf("%d",x++); printf("%d",x); y--; printf("%d %d",x,y); getch (); return 0; }

top related