You are on page 1of 13

Programming Logic

Expressions

B.Bhuvaneswaran, AP (SG) / CSE


9791519152
bhuvaneswaran@rajalakshmi.edu.in
Expressions
 An expression represents a single data item-usually a number.
 The expression may consist of a single entity, such as a constant or
variable, or it may consist of some combination of such entities,
interconnected by one or more operators.
 Expressions can also represent logical conditions which are either
true or false.
 However, in C, the conditions true and false are represented by
the integer values 1 and 0, respectively.

Expressions Rajalakshmi Engineering College 2


Arithmetic Expressions
 An arithmetic expression is a combination of variables, constants,
and operators arranged as per the syntax of the language.

Expressions Rajalakshmi Engineering College 3


Examples
Algebraic Expression C Expression
axb+c a*b+c
(x+y)(x-y) (x+y)*(x–y)
xy x*y/z
z
b2–4ac b*b–4*a*c
s(s-a)(s-b)(s-c) s*(s-a)*(s-b)*(s-c)

Expressions Rajalakshmi Engineering College 4


Evaluation of Expressions
 Expressions are evaluated using an assignment statement.

Expressions Rajalakshmi Engineering College 5


Syntax
 variable = expression;
 Where variable is any C variable name.

Expressions Rajalakshmi Engineering College 6


Evaluation of Expressions
 An expression evaluation usually starts from left and passes
through the expression to the extreme right.
 As it passes, the highest priority operators are encountered first
and then the lower priority operators are encountered.
 The operators of the same precedence are evaluated either from
left to right or from right to left, depending on the level.
 But introducing parenthesis in an expression can change the order
of evaluation.

Expressions Rajalakshmi Engineering College 7


Evaluation of Expressions
 When parenthesis is used, the expression within the parenthesis
assumes highest priority.
 If one or more parenthesis appears in an expression, the left most
parentheses is evaluated first and it passes to the right one by
one.
 Parentheses are usually used to increase the readability of the
program.
 The result of the expression is then replaces the previous value of
the variable on the left-hand side.
 All the variables used in the expression must be assigned values
before evaluation is attempted.

Expressions Rajalakshmi Engineering College 8


Examples
 area = l * b * h;
 root = b * b – 4 * a * c;
 avg = sum / n;

Expressions Rajalakshmi Engineering College 9


Rules for Evaluation of Expression
 First, parenthesized sub expressions from left to right are
evaluated.
 If parentheses are nested, the evaluation begins with the
innermost sub–expression.
 The precedence rule is applied in determining the order of
application of operators in evaluating sub–expressions.
 The associativity rule is applied when two or more operators of
the same precedence level appear in a sub–expression.
 Arithmetic expressions are evaluated from left to right using the
rules of precedence.
 When parentheses are used, the expressions within parentheses
assume highest priority.

Expressions Rajalakshmi Engineering College 10


Operator Precedence & Associativity
Operator Description Associativity Rank (Category)
() Function call 1
Left to Right
[] Array subscript (Highest)
+ Unary plus
- Unary minus
++ Pre increment / Post increment
-- Pre decrement / Post decrement
! Logical negation (NOT)
2
~ Bitwise (1’s) complement Right to left
(Unary)
* Pointer reference (indirection)
& Address
sizeof Size of an object
(returns size of operand in bytes)
(type) Type cast (conversion)
* Multiplication
3
/ Division Left to Right
(Multiplicative)
% Modulus
+ Addition (Binary plus) 4
Left to Right
- Subtraction (Binary minus) (Additive)
<< Left shift 5
Left to Right
>> Right shift (Shift)
< Less than
<= Less than or equal to 6
Left to Right
> Greater than (Relational)
>= Greater than or equal to
== Equality (Equal to) 7
Left to Right
!= Inequality (Not equal to) (Equality)

Expressions Rajalakshmi Engineering College 11


Operator Precedence & Associativity
Operator Description Associativity Rank (Category)
& Bitwise AND 8
Left to Right
(Bitwise AND)
^ Bitwise XOR 9
Left to Right
(Bitwise XOR)
| Bitwise OR 10
Left to Right
(Bitwise OR)
&& Logical AND 11
Left to Right
(Logical AND)
|| Logical OR 12
Left to Right
(Logical OR)
?: Condition expression
13
(a ? x : y means "if a then x, else Right to left
(Conditional)
y")
= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder (modulus)
+= Assign sum
14
-= Assign difference Right to left
(Assignment)
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift
, Comma operator (Evaluate) 15
Left to Right
(Comma)

Expressions Rajalakshmi Engineering College 12


Thank You

You might also like