Operators and Expressions
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
7
1
Outline
 Operands
 Arithmetic Operators
 Unary Operators
 Relational and Logical Operators
 Assignment Operator
 Conditional Operator
 Operator Precedence Groups
Operands
 The data items that operators act upon are called
operands.
 Some operators require two operands(a+b, a-b),
while others act upon only one operand(i++, i--).
Arithmetic Operators
 There are five arithmetic operators in C. They are
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after
integer division
Arithmetic Operators (cont..)
 The % operators is sometimes referred as the
modulus operator.
 The remainder operator (%) requires that both
operands be integers and second operand be
nonzero.
Arithmetic Operators (cont..)
 Division (/) of one integer quantity by another is
referred to as integer division that always results in a
truncated quotient (the decimal portion of the
quotient will be dropped).
 If a division operation is carried out with two
floating-point numbers or with one floating-point
number and one integer, the result will be a floating-
point quotient.
Arithmetic Operators (cont..)
 Suppose that a and b are integer variables and a = 10
and b = 3.
Expression Value
a + b 13
a – b 7
a * b 30
a / b 3
a % b 1
Arithmetic Operators (cont..)
 Suppose that v1 and v2 are floating-point variables
and v1 = 12.5 and v2 = 2.0.
Expression Value
v1 + v2 14.5
v1 – v2 10.5
v1 * v2 25.0
v1 / v2 6.25
Unary Operators
 Unary operator is a class of operator that act upon a
single operand to produce a new value.
 The most common unary operation is unary minus, where
a numerical constant, variable or expression is preceded
my a minus sign.
 Example: -743 -0.2 -3 * (x+y)
 There are two other commonly used unary operators:
1. The increment operator (++)
2. The decrement operator (--)
Unary Operators (cont..)
 Suppose that i is an integer variable that has been
assigned a value of 5.The expression ++i, which is
equivalent to writing i = i + 1, cause the value of i to
be increased to 6. Similarly, the expression --i, which is
equivalent to i = i - 1, cause the (original) value of i to
be decreased to 4.
Unary Operators Example 1
 Lets see an example…
a = 10; b = 20;
x = ++a;
y = b++;
printf(“x = %d a = %dn”,x , a);
printf(“y = %d a = %dn”,y , a);
Unary Operators Example 1 (cont..)
Here, x = ++a statement is equivalent to following two statement
a = a + 1;
x = a;
Thus the value of x will be 11.
On the other hand, y = b++ is equivalent to
y = b;
b = b+1;
So, the value of y will be 20 and b will be 21.
Unary Operators Example 2
Lets do these exercise together:
Here, a = 10; b = 20;
1. x = 50 + ++a;
2. y = 100 + b++;
3. x = a++ - ++a;
Relational and Logical Operators
 There are four relational operators.They are
 Closely associate with the relational operators are
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Operator Meaning
== Equal to
!= Not equal to
Relational and Logical Operators
(cont..)
 Relational Operator example 1: suppose that i, j
and k are integer variables whose values are 1, 2 and
3, respectively.
Expression Interpretation Value
i < j true 1
(i + j) >= k true 1
(j +k) > (i + 5) false 0
k !=3 false 0
j == 2 true 1
Relational and Logical Operators
(cont..)
 Relational Operator example 2: Suppose that i is
an integer variable whose value is 7, f is a floating
variable whose value is 5.5, and c is a character
variable that express the character ‘w’
Expression Interpretation Value
f < 5 true 1
(i + f) <= 10 false 0
c == 119 true 1
c != ‘p’ true 1
C > = 10 * ( i + f) false 0
Relational and Logical Operators (cont..)
 C contains two logical operators. They are..
 && is also referred as logical and
 || is also referred as logical or
Operator Meaning
&& and
|| or
Relational and Logical Operators (cont..)
 logical operators examples 1: Suppose that i is an
integer whose value is 7, f is a floating-point variable
whose value is 5.5, and c is a character variable that
represents the character ‘w’.
Expression Interpretation Value
(i >=6) && (c == ‘w’) true 1
(I >=6) || (c == 119) true 1
(f < 11) && (I >100) false 0
(c != ‘p’) || ((I + f) <=
10)
true 1
Relational and Logical Operators (cont..)
 logical operators examples 1: Suppose that i is an
integer whose value is 7, f is a floating-point variable
whose value is 5.5, and c is a character variable that
represents the character ‘w’.
Expression Interpretation Value
(I + f) <= 10 false 0
(i >=6) && (c == ‘w’) true 1
(c = ‘p’) || ((I + f) <=
10)
false 0
Relational and Logical Operators (cont..)
 logical operators examples 1: Suppose that i is an
integer whose value is 7, f is a floating-point variable
whose value is 5.5, and c is a character variable that
represents the character ‘w’.
Expression Interpretation Value
(I + f) <= 10 false 0
(i >=6) && (c == ‘w’) true 1
(c = ‘p’) || ((I + f) <=
10)
false 0
Assignment Operator
 The most commonly used assignment operator is ‘=’
 identifier = expression
 Example:
 a = 3;
 x = y;
 sum = a + b;
 And so on….
Conditional Operator
 Simple conditional operations can be carried out with
the conditional operator(? :).
 A conditional expression is written in the form
expression 1 ? expression 2 : expression 3
 When evaluating a conditional expression, expression 1
is evaluated first.
 If expression 1 is true, than expression 2 is evaluated and this
become the value of the conditional expression.
 If the expression 1 is false, then expression 3 is evaluated and
this become the value of the conditional expression.
Conditional Operator Example
 Let i = 5
So, flag = (i < 0) ? 0 : 100
Output will be 100 as expression 1 (i < 0) is false.
 Let, f = 8 and g = 9
So, min = (f < g) ? f : g
Output will be 8 as expression 1(f<g) is true.
Operator Precedence Groups
Operator Category Operators Associativity
unary operators - ++ -- ! sizeof(type) R -> L
arithmetic multiply,
divide, remainder
* / % L -> R
arithmetic add and
subtract
+ - L -> R
relational operators < <= > >= L -> R
equality operators == != L -> R
logical and && L -> R
logical or || L -> R
conditional operator ? : R -> L
assignment operators = += -+ *= /= %= R -> L
Lecture 7- Operators and Expressions

Lecture 7- Operators and Expressions

  • 1.
    Operators and Expressions Md.Imran Hossain Showrov (showrovsworld@gmail.com) 7 1
  • 2.
    Outline  Operands  ArithmeticOperators  Unary Operators  Relational and Logical Operators  Assignment Operator  Conditional Operator  Operator Precedence Groups
  • 3.
    Operands  The dataitems that operators act upon are called operands.  Some operators require two operands(a+b, a-b), while others act upon only one operand(i++, i--).
  • 4.
    Arithmetic Operators  Thereare five arithmetic operators in C. They are Operator Purpose + Addition - Subtraction * Multiplication / Division % Remainder after integer division
  • 5.
    Arithmetic Operators (cont..) The % operators is sometimes referred as the modulus operator.  The remainder operator (%) requires that both operands be integers and second operand be nonzero.
  • 6.
    Arithmetic Operators (cont..) Division (/) of one integer quantity by another is referred to as integer division that always results in a truncated quotient (the decimal portion of the quotient will be dropped).  If a division operation is carried out with two floating-point numbers or with one floating-point number and one integer, the result will be a floating- point quotient.
  • 7.
    Arithmetic Operators (cont..) Suppose that a and b are integer variables and a = 10 and b = 3. Expression Value a + b 13 a – b 7 a * b 30 a / b 3 a % b 1
  • 8.
    Arithmetic Operators (cont..) Suppose that v1 and v2 are floating-point variables and v1 = 12.5 and v2 = 2.0. Expression Value v1 + v2 14.5 v1 – v2 10.5 v1 * v2 25.0 v1 / v2 6.25
  • 9.
    Unary Operators  Unaryoperator is a class of operator that act upon a single operand to produce a new value.  The most common unary operation is unary minus, where a numerical constant, variable or expression is preceded my a minus sign.  Example: -743 -0.2 -3 * (x+y)  There are two other commonly used unary operators: 1. The increment operator (++) 2. The decrement operator (--)
  • 10.
    Unary Operators (cont..) Suppose that i is an integer variable that has been assigned a value of 5.The expression ++i, which is equivalent to writing i = i + 1, cause the value of i to be increased to 6. Similarly, the expression --i, which is equivalent to i = i - 1, cause the (original) value of i to be decreased to 4.
  • 11.
    Unary Operators Example1  Lets see an example… a = 10; b = 20; x = ++a; y = b++; printf(“x = %d a = %dn”,x , a); printf(“y = %d a = %dn”,y , a);
  • 12.
    Unary Operators Example1 (cont..) Here, x = ++a statement is equivalent to following two statement a = a + 1; x = a; Thus the value of x will be 11. On the other hand, y = b++ is equivalent to y = b; b = b+1; So, the value of y will be 20 and b will be 21.
  • 13.
    Unary Operators Example2 Lets do these exercise together: Here, a = 10; b = 20; 1. x = 50 + ++a; 2. y = 100 + b++; 3. x = a++ - ++a;
  • 14.
    Relational and LogicalOperators  There are four relational operators.They are  Closely associate with the relational operators are Operator Meaning < Less than <= Less than or equal to > Greater than >= Greater than or equal to Operator Meaning == Equal to != Not equal to
  • 15.
    Relational and LogicalOperators (cont..)  Relational Operator example 1: suppose that i, j and k are integer variables whose values are 1, 2 and 3, respectively. Expression Interpretation Value i < j true 1 (i + j) >= k true 1 (j +k) > (i + 5) false 0 k !=3 false 0 j == 2 true 1
  • 16.
    Relational and LogicalOperators (cont..)  Relational Operator example 2: Suppose that i is an integer variable whose value is 7, f is a floating variable whose value is 5.5, and c is a character variable that express the character ‘w’ Expression Interpretation Value f < 5 true 1 (i + f) <= 10 false 0 c == 119 true 1 c != ‘p’ true 1 C > = 10 * ( i + f) false 0
  • 17.
    Relational and LogicalOperators (cont..)  C contains two logical operators. They are..  && is also referred as logical and  || is also referred as logical or Operator Meaning && and || or
  • 18.
    Relational and LogicalOperators (cont..)  logical operators examples 1: Suppose that i is an integer whose value is 7, f is a floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Expression Interpretation Value (i >=6) && (c == ‘w’) true 1 (I >=6) || (c == 119) true 1 (f < 11) && (I >100) false 0 (c != ‘p’) || ((I + f) <= 10) true 1
  • 19.
    Relational and LogicalOperators (cont..)  logical operators examples 1: Suppose that i is an integer whose value is 7, f is a floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Expression Interpretation Value (I + f) <= 10 false 0 (i >=6) && (c == ‘w’) true 1 (c = ‘p’) || ((I + f) <= 10) false 0
  • 20.
    Relational and LogicalOperators (cont..)  logical operators examples 1: Suppose that i is an integer whose value is 7, f is a floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Expression Interpretation Value (I + f) <= 10 false 0 (i >=6) && (c == ‘w’) true 1 (c = ‘p’) || ((I + f) <= 10) false 0
  • 21.
    Assignment Operator  Themost commonly used assignment operator is ‘=’  identifier = expression  Example:  a = 3;  x = y;  sum = a + b;  And so on….
  • 22.
    Conditional Operator  Simpleconditional operations can be carried out with the conditional operator(? :).  A conditional expression is written in the form expression 1 ? expression 2 : expression 3  When evaluating a conditional expression, expression 1 is evaluated first.  If expression 1 is true, than expression 2 is evaluated and this become the value of the conditional expression.  If the expression 1 is false, then expression 3 is evaluated and this become the value of the conditional expression.
  • 23.
    Conditional Operator Example Let i = 5 So, flag = (i < 0) ? 0 : 100 Output will be 100 as expression 1 (i < 0) is false.  Let, f = 8 and g = 9 So, min = (f < g) ? f : g Output will be 8 as expression 1(f<g) is true.
  • 24.
    Operator Precedence Groups OperatorCategory Operators Associativity unary operators - ++ -- ! sizeof(type) R -> L arithmetic multiply, divide, remainder * / % L -> R arithmetic add and subtract + - L -> R relational operators < <= > >= L -> R equality operators == != L -> R logical and && L -> R logical or || L -> R conditional operator ? : R -> L assignment operators = += -+ *= /= %= R -> L