OPERATORS & EXPRESSIONS
Session 3
OBJECTIVES
• Explain Assignment Operators
• Understand Arithmetic Expressions
• Explain Relational and Logical Operators
• Understand Bitwise Logical Operators and Expressions
• Explain Casts
• Understand the Precedence of Operators
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
2
OPERATORS
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
3
• C has a very rich set of operators.
• Operators are the tools that manipulate data.
• An operator is a symbol, which represents some particular operation to be
performed on the data.
• C defines four classes of operators: arithmetic, relational, logical, and bitwise.
• In addition C has some special operators for special tasks.
• Operators operate on constants or variables, which are called operands.
• binary operators act on two data elements and ternary operators operate on three
data elements.
• For Ex: c = a + b;
Here a, b, c are the operands and ‘=’ and ‘+’ are the operators.
EXPRESSIONS
• An expression can be any combination of operators and operands.
• Operators perform operations like addition, subtraction, comparison etc.
• Operands are the variables or values on which the operations are performed. For
example, in a + b, a and b are operands and + is the operator.
• The whole thing together is an expression.
• The following are examples of expressions:
• 2
• X
• 3 + 7
• 2 × y + 5
• 2 + 6 × (4 - 2) z + 3 × (8 - z)
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
4
ASSIGNMENT OPERATOR
• Before looking into other operators, it is essential to discuss the assignment
operator (=).
• It is the most common operator in any language, and well known to everyone.
• In C, the assignment operator can be used with any valid C expression.
• The general form of the assignment operator is:
• Variablename=expression
• Int num=13;
• Float tempreture=23.222;
• String name =“ali”;
• Many variables can be assigned the same value in a single statement.
• a = b = c =10; // Multiple Assignments
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
5
Operators Example/Description
=
sum = 10;
10 is assigned to variable sum
+=
sum += 10;
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
/=
sum /= 10;
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
^=
sum ^= 10;
This is same as sum = sum ^ 10
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
6
CODE
1
2
3
4
5
6
7
8
9
10
11
# include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
7
ARITHMETIC OPERATOR IN C
• The operations are carried out in a specific (or a particular) order, to arrive at the
final value. This order is known as order of precedence (discussed later).
• Mathematical expressions can be expressed in C using the arithmetic operators with
numeric and character operands.
• Such expressions are known as Arithmetic Expressions.
• a * (b+c/d)/22;
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
8
Arithmetic Operators/Operation Example
+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
CODE
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %dn", add);
printf("Subtraction of a, b is : %dn", sub);
printf("Multiplication of a, b is : %dn", mul);
printf("Division of a, b is : %dn", div);
printf("Modulus of a, b is : %dn", mod);
}
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
9
RELATIONAL OPERATOR
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
10
• Relational operators are used to find the relation between two variables. i.e. to
compare the values of two variables in a C program.
Operators Example/Description
> 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)
CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
11
LOGICAL OPERATOR
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
12
• These operators are used to perform logical operations on the given expressions.
• There are 3 logical operators in C language. They are, logical AND (&&), logical
OR (||) and logical NOT (!).
Operators Example/Description
&& (logical AND)
(x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR)
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (logical NOT)
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false
EXAMPLE
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
13
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are truen");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is truen");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are truen");
}
else
{
printf("! Operator : Both conditions are true. " 
"But, status is inverted as falsen");
}
}
BITWISE OPERATOR
• These operators are used to perform bit operations. Decimal values are converted
into binary values which are the sequence of bits and bit wise operators work on
these bits.
• Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT),
^ (XOR), << (left shift) and >> (right shift).
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
14
CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %dn",AND_opr );
printf("OR_opr value = %dn",OR_opr );
printf("NOT_opr value = %dn",NOT_opr );
printf("XOR_opr value = %dn",XOR_opr );
printf("left_shift value = %dn", m << 1);
printf("right_shift value = %dn", m >> 1);
}Presented by: Muhammad Ehtisham Siddiqui (BSCS)
15
OUTPUT:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
Presented by: Muhammad Ehtisham Siddiqui (BSCS)
16

C programming Tutorial Session 4

  • 1.
  • 2.
    OBJECTIVES • Explain AssignmentOperators • Understand Arithmetic Expressions • Explain Relational and Logical Operators • Understand Bitwise Logical Operators and Expressions • Explain Casts • Understand the Precedence of Operators Presented by: Muhammad Ehtisham Siddiqui (BSCS) 2
  • 3.
    OPERATORS Presented by: MuhammadEhtisham Siddiqui (BSCS) 3 • C has a very rich set of operators. • Operators are the tools that manipulate data. • An operator is a symbol, which represents some particular operation to be performed on the data. • C defines four classes of operators: arithmetic, relational, logical, and bitwise. • In addition C has some special operators for special tasks. • Operators operate on constants or variables, which are called operands. • binary operators act on two data elements and ternary operators operate on three data elements. • For Ex: c = a + b; Here a, b, c are the operands and ‘=’ and ‘+’ are the operators.
  • 4.
    EXPRESSIONS • An expressioncan be any combination of operators and operands. • Operators perform operations like addition, subtraction, comparison etc. • Operands are the variables or values on which the operations are performed. For example, in a + b, a and b are operands and + is the operator. • The whole thing together is an expression. • The following are examples of expressions: • 2 • X • 3 + 7 • 2 × y + 5 • 2 + 6 × (4 - 2) z + 3 × (8 - z) Presented by: Muhammad Ehtisham Siddiqui (BSCS) 4
  • 5.
    ASSIGNMENT OPERATOR • Beforelooking into other operators, it is essential to discuss the assignment operator (=). • It is the most common operator in any language, and well known to everyone. • In C, the assignment operator can be used with any valid C expression. • The general form of the assignment operator is: • Variablename=expression • Int num=13; • Float tempreture=23.222; • String name =“ali”; • Many variables can be assigned the same value in a single statement. • a = b = c =10; // Multiple Assignments Presented by: Muhammad Ehtisham Siddiqui (BSCS) 5
  • 6.
    Operators Example/Description = sum =10; 10 is assigned to variable sum += sum += 10; 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 /= sum /= 10; 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 ^= sum ^= 10; This is same as sum = sum ^ 10 Presented by: Muhammad Ehtisham Siddiqui (BSCS) 6
  • 7.
    CODE 1 2 3 4 5 6 7 8 9 10 11 # include <stdio.h> intmain() { int Total=0,i; for(i=0;i<10;i++) { Total+=i; // This is same as Total = Toatal+i } printf("Total = %d", Total); } Presented by: Muhammad Ehtisham Siddiqui (BSCS) 7
  • 8.
    ARITHMETIC OPERATOR INC • The operations are carried out in a specific (or a particular) order, to arrive at the final value. This order is known as order of precedence (discussed later). • Mathematical expressions can be expressed in C using the arithmetic operators with numeric and character operands. • Such expressions are known as Arithmetic Expressions. • a * (b+c/d)/22; Presented by: Muhammad Ehtisham Siddiqui (BSCS) 8 Arithmetic Operators/Operation Example + (Addition) A+B – (Subtraction) A-B * (multiplication) A*B / (Division) A/B % (Modulus) A%B
  • 9.
    CODE 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> int main() { inta=40,b=20, add,sub,mul,div,mod; add = a+b; sub = a-b; mul = a*b; div = a/b; mod = a%b; printf("Addition of a, b is : %dn", add); printf("Subtraction of a, b is : %dn", sub); printf("Multiplication of a, b is : %dn", mul); printf("Division of a, b is : %dn", div); printf("Modulus of a, b is : %dn", mod); } Presented by: Muhammad Ehtisham Siddiqui (BSCS) 9
  • 10.
    RELATIONAL OPERATOR Presented by:Muhammad Ehtisham Siddiqui (BSCS) 10 • Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program. Operators Example/Description > 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)
  • 11.
    CODE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main() { intm=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m and n are not equal"); } } Presented by: Muhammad Ehtisham Siddiqui (BSCS) 11
  • 12.
    LOGICAL OPERATOR Presented by:Muhammad Ehtisham Siddiqui (BSCS) 12 • These operators are used to perform logical operations on the given expressions. • There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!). Operators Example/Description && (logical AND) (x>5)&&(y<5) It returns true when both conditions are true || (logical OR) (x>=10)||(y>=10) It returns true when at-least one of the condition is true ! (logical NOT) !((x>5)&&(y<5)) It reverses the state of the operand “((x>5) && (y<5))” If “((x>5) && (y<5))” is true, logical NOT operator makes it false
  • 13.
    EXAMPLE Presented by: MuhammadEhtisham Siddiqui (BSCS) 13 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> int main() { int m=40,n=20; int o=20,p=30; if (m>n && m !=0) { printf("&& Operator : Both conditions are truen"); } if (o>p || p!=20) { printf("|| Operator : Only one condition is truen"); } if (!(m>n && m !=0)) { printf("! Operator : Both conditions are truen"); } else { printf("! Operator : Both conditions are true. " "But, status is inverted as falsen"); } }
  • 14.
    BITWISE OPERATOR • Theseoperators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits. • Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift). Presented by: Muhammad Ehtisham Siddiqui (BSCS) 14
  • 15.
    CODE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> int main() { intm = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ; AND_opr = (m&n); OR_opr = (m|n); NOT_opr = (~m); XOR_opr = (m^n); printf("AND_opr value = %dn",AND_opr ); printf("OR_opr value = %dn",OR_opr ); printf("NOT_opr value = %dn",NOT_opr ); printf("XOR_opr value = %dn",XOR_opr ); printf("left_shift value = %dn", m << 1); printf("right_shift value = %dn", m >> 1); }Presented by: Muhammad Ehtisham Siddiqui (BSCS) 15 OUTPUT: AND_opr value = 0 OR_opr value = 120 NOT_opr value = -41 XOR_opr value = 120 left_shift value = 80 right_shift value = 20
  • 16.
    Presented by: MuhammadEhtisham Siddiqui (BSCS) 16