You are on page 1of 34

OPERATORS

CS 110 Fundamentals ofComputer Programming


By Asma Majeed

1
CONTENTS

Relational Operators
Logical Operators
Unary Arithmetic Operators
Assignment Operator
Increment & Decrement
Ternary Operator
Conditional Operator

2
RELATIONAL OPERATORS

• A relational operator checks the relationship


between two operands.

• If the relation is true, it returns 1; if the relation


is false, it returns value 0. Used to compare two
operands to produce a "boolean" result.

• Relational operators are used in decision


making and loops.

3
RELATIONAL OPERATORS

Used to compare two operands to produce a ’boolean’ result.


1 Greater than. > operator
1 3 > 2;/ * Eval uat es t o 1 * /
2 2.99 > 3;/ * evaluates to 0 */

2 Greater than or equal to. >= operator


1 3 >= 3;/ * evaluates to1 */
2 2.99 >= 3;/ * evaluates to 0 */

3 Lesser than. < operator


1 3 < 3;/ * evaluatesto0 */
2 ’A’<’B’;/ * evaluatesto1 */

4 lesser than or equal to. <= operator


1 3 <= 3;/ * evaluatesto1 */
2 3.99 < 3;/ * evaluatesto0 */
4
RELATIONAL OPERATORS(CONTD.)

Also used to test anequality.


1 Equal to. == operator.
1 3 == 3;/ * evaluatesto1 */
2 ’A’==’a’;/ * evaluatesto0 */

2 Not equal to. != operator.


1 3 != 3;/ * Eval uat es t o 0 */
2 2.99 != 3;/ * evaluates to 1 */

Notes
"==" equality operator is different from the "=", assignment
operator.
"==" operator on float variables is tricky because of finiteprecision.
5
LOGICAL OPERATORS

1 AND. && operator


1 ( ( 9/3) == 3) && ( 2* 3 == 6) ; / * eval uat est o1 */
2 (’A’==’a ’) && ( 3==3);/ * evaluatesto0 */

2 OR. || operator
1 2==3 ||’A’==’A’;/ * evaluatesto1 */
2 2.99 >=3 | | 0;/ * evaluatesto0 */

3 NOT. ! operator
1 ! ( 3==3);/ * evaluatesto0 */
2 ! ( 2.99 >=3);/ * evaluatesto1 */

6
LOGICAL OPERATORS

evaluation of an expression is discontinued if the value of a conditional


expression canbe determined early.
1 ( 3==3) | | ( (c=get cha r()) ==’y ’); /* second expression is not evaluated*/

2 ( 0) && ( (x=x+1) > 0 ) ; / * second expression is not evaluated */ ←›

What is the problem in this statement?

7
TRUTH TABLES

Logical Operations && Logical Operations ||


x y Result x y Result
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1

Negation
x Result
!0 1
!1 0

8
CONDITIONAL EXPRESSION

General syntax.
1 i f (cond)
2 x= <expra>;
3 else
4 x= <exprb>;

9
EXERCISES

• Q1: Write a program that asks the user to enter two numbers,
and prints their sum, product, difference, quotient and
remainder.

• Q2: Write a program that inputs three different integers from


the keyboard, then prints the sum, the average, the product, the
smallest and the largest of these numbers. Use only the single-
selection form of the if statement

• Q3: Check if the number entered by the user is positive or


negative. If the number is positive and even print “hurray”
• If the number is negative and odd print “sorry”

10
UNARY ARITHMETIC OPERATORS

• The unary operators require one operand

• i = +1;
• j = -i;

• The unary + operator does nothing. It’s used


primarily to emphasize that a numeric constant
is positive.

12
OPERATOR PRECEDENCE

• Highest: + - (unary)
• * / %
• Lowest: + - (binary)
• Examples:
• i + j * k is equivalent to i + (j * k)
• -i * -j is equivalent to (-i) * (-j)
• +i + j / k is equivalent to (+i) + (j / k)

13
SIMPLE ASSIGNMENT

• The effect of the assignment v = e is


to evaluate the expression e and
copy its value into v.
• e can be a constant, a variable, or a
more complicated expression:
• i = 5; /* i is now 5 */
• j = i; /* j is now 5 */
• k = 10 * i + j; /* k is now 55 */

14
SIMPLE ASSIGNMENT

• If v and e don’t have the same type, then the


value of e is converted to the type of v as the
assignment takes place:

• int i;
• float f;
• i = 72.99f; /* i is now 72 */
• f = 136; /* f is now 136.0 */

15
LVALUES

• The assignment operator requires an lvalue as


its left operand.

• An lvalue represents an object stored in


computer memory, not a constant or the result
of a computation.

• Variables are lvalues; expressions such as 10 or
2 * i are not.

16
LVALUES

• Since the assignment operator requires an lvalue as its


left operand, it’s illegal to put any other kind of
expression on the left side of an assignment expression:

/*** WRONG ***/


• 12 = i;
/*** WRONG ***/
• i + j = 0;
• -i = j; /*** WRONG ***/

• The compiler will produce an error message such as


“invalid lvalue in assignment

17
COMPOUND ASSIGNMENT

• Assignments that use the old value of a variable


to compute its new value are common.
• Example:
• i = i + 2;

• Using the += compound assignment operator,


we simply write:
• i += 2; /* same as i = i + 2; */

18
COMPOUND ASSIGNMENT

• i = i + 2;  i += 2;

• -=
• /=
• *=
• %=

• Right to left associativity

19
EXAMPLE

• int main() { • c *= a; // c = c*a


• printf("c = %d \n", c); c=5
• int a = 5, c; c = a;
• c /= a; // c = c/a c = 10
• printf("c = %d \n", c); • printf("c = %d \n", c);
c=5
• c += a; // c = c+a • c %= a; //c = c%a c = 25
• printf("c = %d \n", c); • printf("c = %d \n", c); c=5
• return 0;
• c -= a; // c = c-a c=0
• }
• printf("c = %d \n", c);

20
INCREMENT AND DECREMENT BY 1

• Two of the most common operations on a variable are


incrementing(adding 1) and decrementing (subtracting
1):

• i = i + 1;  i += 1;  i++;

• i = i – 1;  i -= 1;  i--;

• Right to left associativity

21
INCREMENT AND DECREMENT OPERATORS

• The increment and decrement


operators are tricky to use:
• •They can be used as prefix
operators (++i and –-i) or postfix
operators (i++ and i--).

22
PRE AND POST INCREMENT

• Evaluating the expression ++i (a ―pre-increment‖)


yields i + 1 and—as a side effect—increments i:
• i = 1;
• printf("i is %d\n", ++i); /* prints "i is 2" */
• printf("i is %d\n", i); /* prints "i is 2" */
• Evaluating the expression i++ (a ―post-
increment‖) produces the result i, but causes i to
be incremented afterwards:
• i = 1;
• printf("i is %d\n", i++); /* prints "i is 1" */
• printf("i is %d\n", i); /* prints "i is 2" */
• 23
PRE AND POST INCREMENT

• ++i means ―increment i immediately‖

• i++ means ―use the old value of i for


now, but increment i later

24
PRE AND POST DECREMENT

• The -- operator has similar properties:


• i = 1;
• printf("i is %d\n", --i); /* prints "i is 0" */
• printf("i is %d\n", i); /* prints "i is 0" */

• i = 1;
• printf("i is %d\n", i--); /* prints "i is 1" */
• printf("i is %d\n", i); /* prints "i is 0" */

25
EXAMPLE

• int main() {
• int a = 10, b = 100;
• float c = 10.5, d = 100.5;
• printf("++a = %d \n", ++a);
• printf("--b = %d \n", --b);
• printf("++c = %f \n", ++c);
• printf("--d = %f \n", --d);
• return 0; }

++a = 11 --b = 99 ++c = 11.500000 ++d = 99.500000

26
TABLE OF ARITHMETIC OPERATORS
DISCUSSED SO FAR:

• Precedence Name Symbol(s)


• 1 increment (postfix) ++
• decrement (postfix) --
• 2 increment (prefix) ++
• decrement (prefix) --
• unary plus +
• unary minus -
• 3 multiplicative */%
• 4 additive +-
• 5 assignment = *= /= %=
+= -=

27
EXERCISE

• When ++ or -- is used more than once in the same expression,


the result can often be hard to understand.

• Example:
• i = 1;
• j = 2;
• k = ++i + j++;

• How this statement is evaluated??

28
TERNARY OPERATOR (?:)

• A conditional operator is a ternary


operator, that is,
• it works on 3 operands.

• Conditional Operator Syntax


• conditional Expression ?
expression1 : expression2

29
EXAMPLE

• if (x%2==1)
== isodd = x%2==1 ? 1 : 0;
• isodd = 1
• else
• isodd = 0

30
CONDITIONAL OPERATOR

• isodd = x%2==1 ? 1 : 0;

• The first expression conditional Expression is evaluated first.



• This expression evaluates to
• 1 if it's true and evaluates to 0 if it's false.

• If conditional Expression is true, expression1 is evaluated.


• If conditional Expression is false, expression2 is evaluated.

31
THE SIZEOF OPERATOR

• The sizeof is an unary operator which returns the size of data (constant,
variables, array, structure etc).

• Example
• main(){
• int a; float b; double c; char d;

• printf("Size of int=%d bytes \n",sizeof(a));


• printf("Size of float=%d bytes \n",sizeof(b));
• printf("Size of double=%d bytes \n",sizeof(c));
• printf("Size of char=%d byte \n",sizeof(d));
• return 0;
• }

32
RELATIONAL OPERATOR EXAMPLE

•i < j < k

•((i < j) < k) // Incorrect use

•i < j && j < k // Correct use

33
EXERCISE

• Write a program to prompt the user to input the integral value of a and print
out the result as shown below:

Result:
The value of a is: 10
……………………..
The value of ++a is: 11
Now the value of a is: 11

The value of a++ is: 11


Now the value of a is: 12

The value of --a is:11


Now the value of a is:11

The value of a-- is: 11


Now the value of a is: 10

34
QUESTIONS

35

You might also like