0% found this document useful (0 votes)
38 views22 pages

PL1 Lecture 4 Operators and Expressions

Uploaded by

Mohammed Salah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views22 pages

PL1 Lecture 4 Operators and Expressions

Uploaded by

Mohammed Salah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Operators and Expressions

Course Title: Programming language 1

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 04 Week No: 5 Semester: Spring 23-24


Lecturer: Aiman Lameesa, [email protected]
Lecture Outline

 List of topics,
 Operators
 Types of operators
 Operator precedence
OPERATORS AND EXPRESSIONS

Operators: The symbols which are used to perform logical


and mathematical operations.
Expression: Operators, functions, constants and variables
are combined together to form expressions.
Example: A + B * 5 is a Expression, where, +, * are
operators, A, B are variables, 5 is constant and A + B * 5 is
an expression.
TYPES OF C OPERATORS
C language offers many types of operators.They are,
a.Arithmetic operators
b.Assignment operators
c.Relational operators
d.Logical operators
e.Bit wise operators
f. Conditional operators (ternary operators)
g.Increment/decrement operators
h.Special operators
TYPES OF C OPERATORS
Types of Description
Operators
Arithmetic_operators These are used to perform mathematical calculations like
addition, subtraction, multiplication, division and
modulus
Assignment_operators These are used to assign the values for the variables in C
programs.
Relational operators These operators are used to compare the value of two
variables.
Logical operators These operators are used to perform logical
operations on the given two variables.
Bit wise operators These operators are used to perform bit operations on given
two variables.

Conditional (ternary) Conditional operators return one value if condition is true


operators and returns another value is condition is false.

Increment/decrement These operators are used to either increase or decrease the


operators value of the variable by one.
ARITHMETIC OPERATOR
C Arithmetic operators are used to perform mathematical calculations

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>

int main(){ What is the output?


int x = 10, y = 20;

if(x != y){ x and y are not equal!


printf("x and y are not equal!\
n"); } if(!(x == y)){
x and y are not equal!
printf("x and y are not equal!\ condition satisfied!
n");} condition satisfied!
if(!(x<5)&&!(y>25))
{ printf("condition satisfied!\
n");}

if(!((x<5)&&(y>25)))
{ printf("condition
satisfied!");}

return 0;
}
TERNARY OPERATOR
Syntax : (C ondition? true_value: false_value);
Example : (A > 100 ? 0 : 1);

In above example, if A is greater than 100, 0 is returned else 1 is returned. This is


equal to if else conditional statements.

#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

Increase or decrease the value of a variable by one.

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

#include <stdio.h> #include


int main() <stdio.h>
{ int main()
int i=1; {
while(i<10) int i=20;
{ while(i>10)
printf("%d ",i); {
i++; printf("%d ",i);
} i--;
} }
}

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

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
int i=0; int i=0;
while(++i < 5 ) while(i++ < 5 )
{ {
printf("%d ",i); printf("%d ",i);
} }
return 0; return 0;
} }

Output: 1 2 3 4 Output: 1 2 3 4
5
PRE/POST INCREMENT AND DECREMENT
OPERATOR

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
int i=10; int i=10;
while(--i > 5 ) while(i-- > 5 )
{ {
printf("%d ",i); printf("%d ",i);
} }
return 0; return 0;
} }

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

int main() int main()


{ {
int i = 4; int i = 4;
int j = i + --i; int j = i + i--;
printf("%d\n", j); printf("%d\n", j);
} }

Output: 6 Output: 7

You might also like