Lecture_CSP03

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Operators and Expressions

 Arithmetic Operators
 Relational Operators
Assignment Operators
Logical Operators
Bitwise Operators
 Increment and Decrement Operators
 Conditional Operators
 comma operator
 Special Operators

1
Operators

• Unary

• Binary

• Ternary
Arithmetic Operators

Operator Meaning

+ Addition or unary plus

- Subtraction or unary minus

* Multiplication

/ Division

% Modulo Division
(not applied for float and double)

3
Arithmetic Expression

Let int x = 15; int y = 6;


Example:
x-y = 9
x+y = 21
x*y = 90
x/y = 2 (decimal part truncated)
x%y = 3 (remainder of the division)

Q. If x and y are declared as float then?


Q. If x is integer and y is float then?

4
Example1.c
main()
{ int x, y, z;
x=1;
y=3; z=10;
printf(“ print values of x=%d, y=%d, z=%d“, x, y, z);
x=x+y;
printf(“print value of x=%d”,x);
x=x-y;
printf(“value of x=%d”,x);
x=4; //RESET
z=z*x;
printf(“value of z=%d”,z);
z=15; //Reset
z=z/y;
printf(“value of z=%d”,z);
}
Examples:
Write a equivalent C expression for given algebraic expressions.

1. (a+b)(c+d)
2. ax2+bx+c

6
COMBAINING ARITHMETIC OPERATOR WITH =
Z= X+Y;
X=X+Y;
 The combination of assignment operator (=) with the arithmetic
operators (+,-,*,/, %) gives the arithmetic assignment operator.

Eg. X+=Y; equivalent to  X=X+Y;

arithmetic assignment operator


+= Addition assignment operator
-= Subtraction assignment operator
*= Multiplication assignment operator
/= Division assignment operator
%= Remainder assignment operator
Examples

Z = Z*X+Y;

Z* = X+Y; (True / False)


Examples
Z=Z*X+Y;

Z*= X+Y; ( T / F)

It indeed the same as: Z=Z* (X+Y);


EXAMPLE

X=1; Y=3; Z=10;

X=X+Y; ????

X=1;

X+=Y; ???

X=1;

Z=Z*X+Y; ???

Z=10;

Z=Z*(X+Y); ???

Z=10;

Z*=X+Y ????
EXAMPLE

X=1; Y=3; Z=10;

X=X+Y; ???? 4

X=1; /* RESET */

X+=Y; ??? 4

X=1; /* RESET */

Z=Z*X+Y; ??? 13

Z=10; /* RESET */

Z=Z*(X+Y); ??? 40

Z=10; /* RESET */

Z*=X+Y ???? 40
Unary minus

 -ve number can be represented by (-) symbol.


Z = X - -Y;
Z = X- (-Y); or Z= X+Y;

 Here first symbol used as: Subtraction


 Second symbol used as : unary minus operator.
Assignment Operators
Simple Assignment Operators Shorthand Operators
a = a+1 a+= 1
a = a-1 a-= 1
a =a*(b+1) a*=b+1
a = a/(b+1) a/=b+1
a = a%b a%=b

Question: b*=b; is equivalent to??

Short hand statement is more concise


and easier to read

13
Operators
Associatively
 () [] ->
 */%+-
 << >>
 < <= > >=
 == !=
• Left  Right
 &
 ^
 |
 && ||
 ,
 ?:
 = += -= *= /= %= &= • Right  Left
^= |= <<= >>=
Relational Operators

Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to

15
Examples:

5 <= 7 TRUE
5 < -8 FALSE
11 < 8+4 TRUE
x+y == a+b TRUE if value of x+y is equal to value of a+b
-20 >= 0 FALSE

Q. Are !(a>b) and a<=b same??


Q. Are !(a==b) and a!=b same??

In C, a true/false condition can be represented


as an integer
In C, ``false'' is represented by a value of 0 (zero), and ``true'' is
represented by any value that is nonzero.
16
Take Home Examples:
Write a equivalent C expression for given algebraic expressions.

1. (a+b)(c+d)
2. ax2+bx+c
3. pr2+2prh
4. s = ut + 1/2at2
5. T = (m1m2/m1+m2)

17

You might also like