PL1 Lecture 4 Operators and Expressions
PL1 Lecture 4 Operators and Expressions
List of topics,
Operators
Types of operators
Operator precedence
OPERATORS AND EXPRESSIONS
Arithmetic
Operators/Operation Example
+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
ARITHMETIC OPERATOR
#include <stdio.h>
int main()
{
int a=40,b=20, add, sub, mul, div;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b; Addition of a, b is : 60
printf("Addition of a, b is : %d\n", add); Subtraction of a, b is : 20
printf("Subtraction of a, b is : %d\n", sub); Multiplication of a, b is : 800
printf("Multiplication of a, b is : %d\n", mul);
Division of a, b is : 2
printf("Division of a, b is : %d\n", div);
return 0;
}
ASSIGNMENT OPERATOR
Operators Example/
Description
= sum = 10;
• Used to assign values to variables. 10 is assigned to variable sum
• Two categories of assignment += sum += 10;
operators: This is same as sum = sum +
10
1.Simple assignment operator
-= sum -= 10;
( Example: = )
This is same as sum = sum –
2.Compound assignment 10
operators sum *= 10;
*=
( Example: +=, -=, *=, /=, This is same as sum = sum *
%=, &=, ^= ) 10
/= sum /= 10;
This is same as sum = sum /
10
%= sum %= 10;
This is same as sum = sum %
10
ASSIGNMENT OPERATOR
#include
# include <stdio.h> <stdio.h> int
int main() main(){
{ int num = 1;
int Total=0,i; num =* 10;
for(i=0;i<10;i++)
{ num =/ 10;
Total+=i; // This is same return 0;
as Total = Total+i }
}
#include
printf("Total = %d", Total);
<stdio.h> int
}
main(){
int num = 1;
num *= 10;
Total = 45
num /= 10;
return 0;
}
RELATIONAL OPERATOR
Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables
Operator Example/Description
s
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal
to y)
<= x <= y (x is less than or equal to
y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
RELATIONAL OPERATOR
#include <stdio.h>
int main()
{
int m=40,n=20; What will be the output!?
if (m == n)
{
printf("m and n are m is greater than n
equal");
}
else if (m < n)
{
printf("m is less than n");
}
else if (m > n)
{
printf("m is greater than
n");
}
LOGICAL OPERATOR
• These operators are used to perform logical operations on the given expressions.
• There are 3 logical operators in C language.
Operators Example/Description
(x>5)&&(y<5)
&& (logical AND) It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
! (logical NOT) It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it
false
LOGICAL OPERATOR
#include <stdio.h>
int main()
{
int m=40,n=20, o=20,p=30;
if (m>n && m !=0) && Operator : Both conditions are
{
printf("&& Operator : Both conditions are
true
true\n"); || Operator : Only one condition is
}
if (o>p || p!=20) true
{
printf("|| Operator : Only one condition is
! Operator : Both conditions are
true\n"); true. But status is inverted as false
}
if (!(m>n && m !=0))
{
printf("! O perator : Both conditions are
true\n");
}
else
{
printf("! O perator : Both conditions are
true.
But status is inverted as false");
}
LOGICAL OPERATOR
#include <stdio.h>
if(!((x<5)&&(y>25)))
{ printf("condition
satisfied!");}
return 0;
}
TERNARY OPERATOR
Syntax : (C ondition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
#include <stdio.h>
int main()
{
x value is 1
int x=1, y ; y value is 2
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
IN CREMEN T AN D DECREMENT O PERATO R
Syntax:
Increment operator: ++var_name; (or) var_name+
+;
Decrement operator: – -var_name; (or) var_name –
-;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ;i – – ;
IN CREMEN T AN D DECREMENT O PERATO R
Output: 1 2 3 4 5 6 7 8 9 Output: 20 19 18 17 16 15 14 13 12 11
PRE/POST INCREMENT AND DECREMENT
OPERATOR
Operator Operator/Description
Pre increment operator value of i is incremented before assigning it to
(++i) the variable i
Post increment operator
value of i is incremented after assigning it to
(i++)
the variable i
Pre decrement operator
value of i is decremented before assigning it
(- –i)
to the variable i
Post decrement value of i is decremented after assigning it to
operator variable i
(i– -)
PRE/POST INCREMENT AND DECREMENT
OPERATOR
Output: 1 2 3 4 Output: 1 2 3 4
5
PRE/POST INCREMENT AND DECREMENT
OPERATOR
Output: 9 8 7 Output: 9 8 7 6 5
6
Operator Description Associativity
() Parentheses (function call) (see Note 1) left-to-right
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note 2) C operators in order of
++ -- Prefix increment/decrement right-to-left
precedence (highest to lowest).
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
Their associativity indicates in
& Address (of operand)
sizeof Determine size in bytes on this implementation what order operators of equal
* / % Multiplication/division/modulus left-to-right precedence in an expression are
+ - Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
applied.
< <= Relational less than/less than or equal to left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right Note 1:Parentheses are also used to group sub-
| Bitwise inclusive OR left-to-right expressions to force a different precedence; such
parenthetical expressions can be nested and are
&& Logical AND left-to-right evaluated from inner to outer.
|| Logical OR left-to-right
?: Ternary conditional right-to-left Note 2:Postfix increment/decrement have high
precedence, but the actual increment or decrement of
= Assignment right-to-left the operand is delayed (to be accomplished sometime
+= -= Addition/subtraction assignment before the statement completes execution). So, in the
*= /= Multiplication/division assignment statement y
= x * z++; the current value of z is used to evaluate
%= &= Modulus/bitwise AND assignment
the expression
^= |= Bitwise exclusive/inclusive OR assignment (i.e., z++ evaluates to z) and z only incremented after
<<= >>= Bitwise shift left/right assignment all else is done.
, Comma (separate expressions) left-to-right
OPERATOR PRECEDENCE
Output: 6 Output: 7