CSE103:
Structured Programming
Lecture 5
Lecture 5
Lecture Contents
• Operators, operands, expressions
• Types of operators
• Type Casting
• Assignment
Lecture 5
Operands
• An operand specifies an entity on
which an operation is to be performed.
• An operand can be a variable name, a
constant.
• For example, a=b+2 is a valid
expression involving three operands
namely
– Two variables name i.e. a,b
– a constant i.e. 2.
Lecture 5
Operators
• An operator specifies the operation to
be applied to its operands.
• For example, the expression:
– a=b+2; involves two operators namely
– arithmetic addition operator i.e. +
– assignment operator i.e. =.
Lecture 5
Expressions
• An expression in C is made up of one or more
operands.
• For example,
– a=2+3 // meaningful expression
– Involves three operands :a, 2 and 3
– Two operators i.e. = (assignment operator) and +
(arithmetic addition operator).
• Thus, an expression is a sequence of operands and
operators that specifies the computation of a
value.
Lecture 5
Simple VS compound
Simple Compound
An expression that has only one An expression that involves more than one
operator is known as simple operator is called compound expression.
expression.
E.g., a+2 is a simple expression. E.g. b=2+3*5 is a compound expression.
Evaluation of simple expression is Whereas the evaluation of a compound
easier as order of determination is expression requires the correct order in
trivial in this case as there is only one which the operators will operate. This
operator depends upon the Precedence and the
Associativity of operators.
Lecture 5
Arithmetic Operators
• The arithmetic operations like addition,
subtraction, multiplication, division etc. can be
performed by using arithmetic operators
◻ Addition + sum = num1 + num2;
◻ Subtraction - age = 2013 – my_birth_year;
◻ Multiplication * area = side1 * side2;
◻ Division / avg = total / number;
◻ Modulus % lastdigit = num % 10;
Lecture 5
Arithmetic Operators (cont’d)
• Note that ‘id = exp‘ means assign
the result of exp to id, so
• X=X+1 means
– first perform X+1 and
– Assign the result to X
• Suppose X is 4, and 4 X
5
• We execute X=X+1
Lecture 5
Arithmetic Operators
Lecture 5
Arithmetic Operators
Lecture 5
Integer division vs Real division
• Division between two integers results in an
integer.
• The result is truncated, not rounded
• Example:
– int A=5/3; 🡪 A will have the value of 1
– int B=3/6; 🡪 B will have the value of 0
• To have floating point values:
– double A=5.0/3; 🡪 A will have the value of 1.666
– double B=3.0/6.0; 🡪 B will have the value of 0.5
Lecture 5
Division
• If both operands of a division
expression are integers, you will get an
integer answer. The fractional portion
is thrown away.
• Examples :
– 17 / 5 = 3
–4 / 3 = 1
– 35 / 9 = 3
Lecture 5
Division (con’t)
• Division where at least one operand is a
floating point number will produce a
floating point answer.
• Examples :
– 17.0 / 5 = 3.4
– 4 / 3.2 = 1.25
– 35.2 / 9.1 = 3.86813
• What happens? The integer operand is
temporarily converted to a floating point,
then the division is performed.
Lecture 5
Division By Zero
• Division by zero is mathematically
undefined.
• If you allow division by zero in a
program, it will cause a fatal error.
Your program will terminate execution
and give an error message.
• Non-fatal errors do not cause
program termination, just produce
incorrect results.
Lecture 5
Modulus
• The expression m % n yields the integer
remainder after m is divided by n.
• Modulus is an integer operation -- both
operands MUST be integers.
• Examples :17 % 5 = 2
● 6%3 = 0
● 9%2 = 1
● 5%8 = 5
Lecture 5
Uses for Modulus
• Used to determine if an integer value is even
or odd
– 5 % 2 = 1 odd 4 % 2 = 0 even
– If you take the modulus by 2 of an integer, a
result of 1 means the number is odd
– and a result of 0 means the number is even.
Lecture 5
Rules of Operator
Precedence
Operator(s) Precedence & Associativity
() Evaluated first. If nested (embedded), innermost
first. If on same level, left to right.
* / % Evaluated second. If there are several, evaluated
left to right.
+ - Evaluated third. If there are several, evaluated
left to right.
= Evaluated last, right to left.
Lecture 5
Examples
Mixed operations:
int a=4+6/3*2; 🡪 a=? a= 4+2*2 = 4+4 = 8
int b=(4+6)/3*2; 🡪 b=? b= 10/3*2 = 3*2= 6
Lecture 5
Extended Example
◻ Given integer variables a, b, c, d, and e, where
a = 1, b = 2, c = 3, d = 4, evaluate the following
expression:
e=b%d/c*b–a
Answer is -1
Lecture 5
Arithmetic Expressions
Algebraic expression C expression
axb-c a*b-c
(m+n)(x+y) (m+n)*(x+y)
a*b/c
3x2+2x+1 3*x*x+2*x+1
a/b
S=
S=(a+b+c)/2
Lecture 5
Relational Operators
• Relational operators compare two operands to
produce a boolean result i.e. 0 or 1.
• In C any non-zero value (1 by convention) is
considered to be ’true’ and 0 is considered to be
false.
Lecture 5
Assignment Operators
• Another common expression type found while
programming in C that assign value.
• Form
– var = var (op) expr
Lecture 5
Assignment Operators
• We can use some short form of assignment operator
• Ex: x=x+3
x+=3
Simple assignment
Shorthand operator
operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
Lecture 5
Increment & Decrement Operators
• C supports 2 useful operators namely
– Increment (++) operator
– Decrement (--) operators
• The ++ operator adds a value 1 to the operand
• The – – operator subtracts 1 from the
operand
• Example:
– ++a or a++
– --a or a--
Lecture 5
Increment & Decrement Operators
• When postfix either ++ or – – is used with
the variable in a given expression, the
expression is evaluated first and then it is
incremented or decremented by one.
• When prefix either ++ or – –is used with the
variable in a given expression, it is
incremented or decremented by one first
and then the expression is evaluated with
the new value.
Lecture 5
More but important
Lecture 5
Increment & Decrement Operators
• Let the value of a =5 and b=++a then
– a = b =6
• Let the value of a =5 and b=a++ then
– b =5 but a=6
• prefix operator first adds 1 to the operand and then the
result is assigned to the variable on the left
• postfix operator first assigns the value to the variable on
left and then increments the operand.
Lecture 5
Example
Lecture 5
Logical operators
• Logical operators are used to logically relate the
expressions
• Logical expression or a compound relational
expression-
– An expression that combines two or more relational
expressions
• Example
– if (a==b && b==c)
Lecture 5
Logical Operators
Lecture 5
Logical operators
• You want to give bonus to a employee
age greater than 45 years and salary
greater than10000.
• C code:
if ( age > 45 && salary > 10000 )
printf( “Give him/her a bonus \n");
else
printf( “No man…\n");
Lecture 5
Lecture 5