Operators and Expressions
Operators and Expressions
EXPRESSIONS
OPERATOR
“is a symbol (+,-,*,/) that directs the computer to perform certain
mathematical or logical manipulations and is usually used to manipulate
data and variables”
Ex: a+b
OPERATORS IN C
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
ARITHMETIC
OPERATORS
Operator Example Meaning
+ a+b Addition –unary
- a–b Subtraction- unary
* a*b Multiplication
/ a/b Division
% a%b Modulo division- remainder
RELATIONAL OPERATORS
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal
to
== Equal to
!= Not equal to
ASSIGNMENT OPERATORS
Syntax:
v op = exp;
where
v = variable,
op = shorthand assignment operator
exp = expression
Example:
x=x+3
x += 3
SHORTHAND ASSIGNMENT
OPERATORS
Simple assignment
operator Shorthand operator
a = a+1 a += 1
a = a-1 a -= 1
a = a* (m+n) a *= m + n
a = a / (m+n) a /= m + n
a = a %b a %= b
INCREMENT & DECREMENT
OPERATORS
C supports 2 useful operators namely
1. Increment ++
2. Decrement -- operators
The ++ operator adds a value 1 to the operand
The -- operator subtracts 1 from the operand
++a or a++
--a or a--
RULES FOR ++ & -- OPERATORS
m=2;
n=3
r=(m>n) ? m : n;
EXAMPLE: INTEGER
OPERANDS d = (x < y);
#include<stdio.h>
void main(void)
e = (x >= y);
{ f = (x <= y);
int a, b, c, d, e, f;
int x, y; cout<< “a = ”<< a<<endl;
cout<< “b = ”<< b<<endl;
x = 5;
Y = 10;
cout<< “c = ”<< c<<endl;
cout<< “d = ”<< d<<endl;
a = (x == y); cout<< “e = ”<< e<<endl;
b = (x != y); }
c = (x > y);
09/15/2023
BITWISE OPERATORS
These operators allow manipulation of data at the bit level
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
SPECIAL OPERATORS
1. Comma operator ( , )
2. sizeof operator – sizeof( )
3. Pointer operators – ( & and *)
4. Member selection operators – ( . and ->)
ARITHMETIC EXPRESSIONS
Algebraic expression C expression
1. A x b - c a*b-c
2. (m + n)(x + y) (m + n) *(x + y)
a*b/c
3.
4. 3x2+2x+1 3*x*x+2*x+1
5. a/b
6. S = (a + b + c) / 2
S=
ARITHMETIC EXPRESSIONS
Algebraic expression C expression
Sin
sin(b / sqrt(a * a + b * b))
09/15/2023
SEMANTICS OF THE !
OPERATOR
!0 results in 1
!1 results in 0
09/15/2023
SEMANTICS OF THE &&
OPERATOR
0 && 0 results in 0
0 && 1 results in 0
1 && 0 results in 0
1 && 1 results in 1
09/15/2023
SEMANTICS OF THE ||
OPERATOR
0 || 0 results in 0
0 || 1 results in 1
1 || 0 results in 1
1 || 1 results in 1
09/15/2023
SEATWORK ( WRITE YOUR
ANSWER IN A 1 WHOLE SHEET OF
PAPER)
A. Write a C program that will declare variables (using the appropriate
data type) and assign values to these variables for the following
problems. You can use any constant value for initialization purposes.
1. The net income is computed as the gross income minus the income
tax, SSS contribution and medical insurance.
2. The salary of a part time worker is computed as the product of the
hourly rate and the number of hours worked.
3. The area of a rectangle is computed as length multiplied by width.
4. The circumference of the circle is computed as 2 multiplied by PI ( a
constant value approximately as 3.1416) multiplied by the radius.
5. Compute the sum of the integer values 1,3,5,7,8, and 9.
09/15/2023
B.DETERMINE IF THE
STATEMENT IF TRUE OR
FALSE
1. (‘ A ’ > ‘ B ’)
2. (123 == 321)
3. (1 + 2 + 3 >= 3 + 2 + 1)
4. (5.0 <= 10.0 / 2.0)
5. (3 + 5 * 10 != 3 * 5 +10)
6. (‘ A ‘ == 65)
09/15/2023