Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

INTRODUCTION TO COMPUTER PROGRAMMING

BTC 1122/ETC 1112


Uthpala Samaratunga
Lecturer
Dept. of ICT, Faculty of Technology
University of Sri Jayewardenepura
uthpalas@sjp.ac.lk

OPERATORS, EXPRESSIONS AND TOPIC 4


STATEMENTS
WHAT IS AN OPERATOR?
An operator is a symbol that tells the compiler to perform a certain
mathematical, logical or other type of manipulation. Operators are
used in programs to manipulate data and variables.
TYPES OF OPERATORS
The operators in C can be classified into following types:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Special operators
WHAT IS AN EXPRESSION?
An expression consists of a combination of operators and
operands. An operand is what an operator operates on. The
simplest expression is a lone operand, and you can build in
complexity from there.
Examples
WHAT IS A STATEMENT?
Statements are the primary building blocks of a program. A
program is a series of statements with some necessary punctuation.
A statement is a complete instruction to a computer. In C, statements
ends with a semicolon which is called the statement terminator.
Examples
ARITHMETIC OPERATORS
INTEGER DIVISION AND THE REMAINDER
OPERATOR
Integer division yields an integer result.
For example, the expression 7/4 evaluates to 1 and the expression
17/5 evaluates to 3.
C provides the remainder operator, %, which yields the remainder
after integer division. Thus, 7%4 yields 3 and 17%5 yields 2.
7/4.0 or 7.0/4 or 7.0/4.0 yields an output with decimal points:
1.75
EXAMPLE: INTEGER DIVISION AND REMAINDER

Output: 5 6
RULES OF OPERATOR PRECEDENCE
INCREMENT AND DECREMENT OPERATORS
INCREMENT AND DECREMENT OPERATORS
++ is the increment operator
 a++
 ++a
Both of the above expressions are similar to a = a + 1. What is
the difference?
-- is the decrement operator
 a--
 --a
Both of the above expressions are similar to a = a - 1. What is the
difference?
INCREMENT AND DECREMENT OPERATOR
Pre increment/ Pre decrement: The increment or decrement
happens before ‘a’ is used in an expression.
 --a
 ++a
Post increment/ Post decrement: The increment or decrement
happens after ‘a’ is used in an expression.
 a++
 a--
QUESTION: WHAT ARE THE VALUES OF A AND B AT THE
END OF EACH OF THE FOLLOWING QUESTIONS?

➢ int a = 10; ➢ int a = 10;


int b = ++a; int b = --a;

➢ int a = 10; ➢ int a = 10;


int b = a++; int b = a--;
ANSWER: WHAT ARE THE VALUES OF A AND B AT THE
END OF EACH OF THE FOLLOWING QUESTIONS?

➢ int a = 10; ➢ int a = 10;


int b = ++a; int b = --a;

a is 11 and b is 11 a is 9 and b is 9

➢ int a = 10; ➢ int a = 10;


int b = a++; int b = a--;
a is 11 and b is 10 a is 9 and b is 10
QUESTION: WHAT ARE THE VALUES OF A AND B AT THE
END OF EACH OF THE FOLLOWING QUESTIONS?

➢ int a = 10; ➢ int a = 10;


int b = ++a + 1; int b = --a + 1;

➢ int a = 10; ➢ int a = 10;


int b = a++ + 1; int b = a-- + 1;
ANSWER: WHAT ARE THE VALUES OF A AND B AT THE
END OF EACH OF THE FOLLOWING QUESTIONS?

➢ int a = 10; ➢ int a = 10;


int b = ++a + 1; int b = --a + 1;

a is 11 and b is 12 a is 9 and b is 10

➢ int a = 10; ➢ int a = 10;


int b = a++ + 1; int b = a-- + 1;
a is 11 and b is 11 a is 9 and b is 11
EQUALITY AND RELATIONAL OPERATORS
QUESTION: WHAT ARE THE OUTPUTS OF
FOLLOWING EXPRESSIONS?
1. 83 > 85
2. 5 == 3
3. 3 != 7
4. 5 <= 9
5. ‘a’ == ‘A’
6. 7 == 7
LOGICAL OPERATORS
C provides logical operators that may be used to form more
complex conditions by combining simple conditions. The logical
operators are:
 ! (logical NOT)
 && (logical AND)
 || (logical OR)
TRUTH TABLE FOR THE LOGICAL AND (&&)
OPERATOR
TRUTH TABLE FOR THE LOGICAL OR (||)
OPERATOR
TRUTH TABLE FOR THE LOGICAL NOT (!)
OPERATOR
QUESTION: WHAT ARE THE OUTPUTS OF
FOLLOWING EXPRESSIONS?
1. !(5 > 7)
2. (5 > 7) && (6 <10)
3. (4 == 5) || (6 != 7)
4. !3
5. !(5 – 5)
6. 3 && -1
7. (6 > -4) || 7
ASSIGNMENT OPERATORS
EXAMPLE: ASSIGNMENT OPERATORS

Output: 2 1

The output value of the variable ‘b’ is 1. It is


not 2. Why?
BITWISE OPERATORS
EXAMPLE: BITWISE OPERATORS
12 & 25

12 = 00001100 (In binary)


25 = 00011001 (In binary)

00001100 &
00011001
00001000 = 8 (In decimal)
BITWISE OPERATORS
x and y are bits

x y x&y x|y x^y ~x


0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
QUESTION: WHAT IS THE VALUE OF C AT THE END
OF EACH STATEMENT?
int a = 10; b = 3, c = 0;

c = a | 2;
c = b & 8;
c = a ^ b;
SPECIAL OPERATORS: SIZEOF() OPERATOR
The sizeof() operator returns the size of a variable (the number of
bytes). For an example, sizeof(a), where a is an integer, will return
4.
OPERATOR
PRECEDENCE AND
ASSOCIATIVITY
TYPE CONVERSIONS: IMPLICIT CONVERSION
When an operator has operands of different types, they are
converted to a common type according to a small number of rules.
Usually the operand having the lower data type will be converted
into a higher data type. Some of the rules are:

•If either operand is double, all the operands are converted to double.
•If either operand is float, all the operands are converted to is float.
IMPLICIT CONVERSION: EXAMPLE

Output: 5

float variable ‘a’ is


implicitly converted
to an int.
IMPLICIT CONVERSION: EXAMPLE

Output: 35.5

int * float
One of the operands is
float, therefore both of
the operands of the
expression are implicitly
converted to float
before evaluating.
float * float
TYPE CONVERSION: EXPLICIT CONVERSION
It is possible for you to demand the precise type conversion that
you want in C. The method of doing this is called ‘type casting’. It
consists of preceding the quantity with the name of the desired
type in parenthesis.
Examples
EXPLICIT CONVERSION: EXAMPLE
Output: 2.583333…

int variable ‘a’ is explicitly


converted to a float.

Since one of the operands


of the expression is now
float, both the operands
are implicitly converted to
float before evaluating.
EXPLICIT CONVERSION: EXAMPLE
You have to choose which is more suitable depending upon the
problem at hand.

Output: 3 2
QUESTIONS: WHAT ARE THE OUTPUTS OF THE
FOLLOWING EXPRESSIONS?
int a = 5;
int b = 10;
1. !(a > b) && (b <= 100)
2. ++a * b % 70
3. 7 | 10
4. 56 * 10 % 2
5. a && b
QUESTIONS: WHAT ARE THE OUTPUTS OF THE
FOLLOWING EXPRESSIONS?
int a = 5;
int b = 10;
1. !(5 > 7) && (10 < 8) + 100
2. ++a + b * 7 & 10
3. 7 % b++ * 7 & (7 <= 2)
4. 56 / a == 5
5. a+=5 + 101
QUESTIONS
1. Write a C program to read a character from the keyboard and
print its ASCII value.
2. Write a C program to read an ASCII value from the keyboard
and print its corresponding character.
Q&A

You might also like