0% found this document useful (0 votes)
4 views6 pages

Lesson 7

This lesson covers various types of operators in programming, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. It also discusses operator precedence and associativity, which dictate the order of operations in expressions. By the end of the lesson, learners should be able to identify and use these operators effectively in their programming tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Lesson 7

This lesson covers various types of operators in programming, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. It also discusses operator precedence and associativity, which dictate the order of operations in expressions. By the end of the lesson, learners should be able to identify and use these operators effectively in their programming tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Faculty of computing and information management

Programming methodology
Lecturer: Mr. HAGGAI
Lesson 7: Operators

7.1. Introduction
In our previous lesson, we discussed about data types, variables and constants. In
our lesson today, we will discuss operators. An operator is a symbol that tells the
compiler to perform specific mathematical or logical manipulations. It defines the
type of calculation to be performed on the operands. In this lesson we will learn
about different types of calculation operators you will be using in your programs.
7.2. Lesson objective
By the end of this lesson the learner will be able to:
ƒ Identify and describe the calculation operators
ƒ Describe the order of precedence of operators
ƒ Write programs using calculation operators
7.3. Lesson outline
This lesson is organized as follows:
5.1. Introduction
5.2. Lesson objectives
5.3. Lesson outline
5.4. Arithmetic operators
5.5. Relational operators
5.6. Logical operators
5.7. Bitwise operators
5.8. Assignment operators 5.9. Misc operators
5.10. Order of precedence
5.11. Associativity of operators
5.12. Revision questions
5.13. Summary
5.14. Suggested reading
7.4.Arithmetic Operators
The arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication, division e.t.c.
Operat Description Exampl
or e
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Remainder of after an integer division x%y
++ increases integer value by one x++
1
-- Decreases integer value by one x--

7.5.Relational operators
These operators are used to compare two operands and determine validity of a
relationship.
Operat Description Example
or
== Equal to If (x== y)
!= Not equal to if(x != y)
> Greater than if(x> y)
< Less than if(x < y)
>= Greater than or equal If (x >= y)
to
<= Less than or equal to if(x <= y).

7.6.Logical operators
These are operators used to compare Boolean expressions and return a Boolean
result. They are used to combine expressions containing relation operators .

Operator Description Example


&& Logical AND operator. if((x==y)&&(y>z))
|| Logical OR Operator. If((x>0)||(y==z))
! Logical NOT Operator. If(!(x && y)).

7.7.Bitwise Operators
A bitwise operator works on each bit of data. Bitwise operators are used in bit level
programming.
Operators Name of Explanation Exampl
operator e
& AND Copies a bit to the result if it exists in both (x&y)
operands
| OR Copies a bit to the result if it exists in either (x|y)
operands
^ Exclusive XOR Copies the bit if it is set in one operand but not (x^y)
both
~ complement Flips the bits(0s change to 1s) (~y)
<< Shift left Left operands value is moved left by number of bits (x<<2)
specified by number of bits specified by right
operand.
>> Shift right Left operands value is moved right by number of (x>>y)
bits specified by number of bits specified by right
operand.

2
7.8. Assignment operators
They are used to assign a value to a variable.
Operato Name of operator Example Same as
r
= Assignment a=b a=b
+= Add AND Assignment a+=b a=a+b
-= Subtract AND Assignment a-=b a=a-b
*= Multiply AND Assignment a*=b a=a*b
/= Divide AND Assignment a/=b a=a/b
%= Modulus AND Assignment a%=b a=a%b
<<= Left shift AND Assignment a<<=b a=a<<b
>>= Right shift AND Assignment a>>=b a=a>>b
&= Bitwise AND AND Assignment a&=b a=a&b
^= Bitwise Exclusive OR AND a^=b a=a^b
Assignment
|= Bitwise OR AND Assignment a|=b a=a|b

7.9. Misc operators


These are operators that perform calculations other than those performed by
arithmetic, relational, logical, bitwise and assignment operators.

Operato Description Example


r
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of an &a; will give actual address of the variable.
variable.
* Pointer to a variable. *a; will point to a variable.
?: Conditional Expression If Condition is true ? Then value X : Otherwise
value Y
7.10. Operator precedence
If more than one operator is involved in an expression then, C language has
predefined rule of priority of operators. This rule of priority of operators is called
operator precedence.
In C, precedence of arithmetic operators(*,%,/,+,-) is higher than relational
operators(==,!=,>,<,>=,<=) and precedence of relational operator is higher than
logical operators(&&, || and !).
7.11. Categories of operators
All the operators can be grouped in the following categories:
Category Explanation
Postfix operators Operators that follow a single operand e.g. a+
Unary operator Operators that precede a single operand e.g. +a
Binary operators Operators that take two operands and perform a variety of arithmetic
or logical operations.
Conditional Operators that take three operands and evaluates either the second or

3
(ternary) operator the third expression depending on the evaluation of the first
expression.
Assignment Operators that assign a value to a variable.
operators
Comma operators: Operator which guarantees left-to-right evaluation of a comma
separated expressions.

7.12. Associativity of operators


Associativity indicates in which order two operators of same precedence (priority)
executes. Example :
a==b!
Here, operators == and != have same precedence. The associativity of both ==
and != is left to right, i.e, the expression in left is executed first and execution take
pale towards right. Thus, a==b!=c equivalent to
(a==b)!

Category Operator Associativit


y
Postfix () [] -> . ++ - - Left to
right
Unary + - ! ~ ++ - - (type)* & Right to
sizeof left
Multiplicativ */% Left to
e right
Additive +- Left to
right
Shift << >> Left to
right
Relational < <= > >= Left to
right
Equality == != Left to
right
Bitwise AND & Left to
right
Bitwise XOR ^ Left to
right
Bitwise OR | Left to
right
Logical AND && Left to
right
Logical OR || Left to
right
Conditional ?: Right to
left
Assignment = += -= *= /= %=>>= Right to

4
<<= &= ^= |= left
Comma , Left to
right

Exercise 1:
Write a program in c that prompts the user to enter two integers and then performs
the following calculations and display result.
i). Add the two integers
ii). Multiply the two integers
iii). Subtract the first integer from the first integer
iv). Divide the first integer by the second integer
v). Calculate the remainder of first integer divided by the first integer
vi). Increment the first integer by one
vii). Decrement first integer by one.
Exercise 2 :
Write a program in c that is used to compare user input with a constant value say
60000.00

Exercise 3:
Write a program in c that is used to calculate discount on cost of products using

Cost Discount
0- 500 1%
5001- 0 1%
0
10001-
10000 2%
5
Exercise 4: 0
100000
Write a program in c that assigns twodvariables
B initial values
A 60 and 13 respectively and
an
following criteria. then
performs and displays results for:
i). A&B
ii). A|B
iii). A^B
iv). ~A
v). A<<2
vi). A<<2
Exercise 5:
Write a program in c that uses following Misc operators.
Exercise 6:
Write a program in c that returns discount of 400 if cost of the product is equal to or
exceeds 2000 otherwise 0.
7.13. Revision questions
a) Using examples discuss the following operators.
[6 Marks]
i). Arithmetic operators
ii). Relational operators

5
iii). Logical operators
iv). Bitwise operators
v). Assignment operators
vi). Misc operators
b) Define modulus operator. Explain the limitations of modulus operator. [3
Marks]
c) Using your knowledge of precedence in operators and given that a=10,b=5,
c=3,d=7 and e=2 rewrite the expression a*b-c/(d%e) so that it evaluates
to: [3 Marks]
i). 20 ii).
50
d) Write a program in c to calculate discount as follows: [6 Marks]
Cost Discount
Below 1000 0
Between 1000 and 5000 3%
Above 15000 7%
e) Determine the hierarchy of operations and evaluate the following expression.
K=3/2*4+3/8+3
[3 Marks]
7.14. Summary
In this lesson you have learnt about calculation operators such as arithmetic,
relational, logical, assignment, bitwise and misc operators. For each category of
operators there are several types that can be used for a given calculation. Finally
you have learnt about the order of precedence of these operators.
7.15. Suggested reading
[1]. An introduction to programming by Wu Thomas, Norman and Theodore: McGrill
(1997).
[2]. C programming language by Brian W. Kernighan, Dennis Ritchie, 2 nd ed: prentice
hall(2003).
[3]. C how to program by H.M. Deitel and P.J.Deitel, 3rd ed: Prentice hall(2001).

You might also like