0% found this document useful (0 votes)
2 views

Fundamentals of Programming lecture#02 (1)

Chapter 5 discusses operators and expressions in programming, emphasizing their role in converting input data into output information through mathematical operations. It covers various types of operators including arithmetic, assignment, relational, and logical operators, along with their usage and examples. The chapter also explains expressions as collections of operands and operators that evaluate to a single value, and highlights the importance of data type conversion in arithmetic expressions.

Uploaded by

sufyanshahid319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Fundamentals of Programming lecture#02 (1)

Chapter 5 discusses operators and expressions in programming, emphasizing their role in converting input data into output information through mathematical operations. It covers various types of operators including arithmetic, assignment, relational, and logical operators, along with their usage and examples. The chapter also explains expressions as collections of operands and operators that evaluate to a single value, and highlights the importance of data type conversion in arithmetic expressions.

Uploaded by

sufyanshahid319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter No.

05: Operators And Expressions

• Operators: The processing phase of a program converts input data into


information that is output. This process of converting data into information takes
many forms. When working with numbers, the process often involves some form
of mathematical operation. Operators are needed to perform such type of
calculations that transform data into information.
Example:
Marks: computer 75, Mathematics 80, Urdu 60, English 70 Data
75+80+60+70=285/400=0.7125*100=71.25%= A Grade information
Operands: Operators are used to perform some ‘operation’ on the ‘operands. For
example a=b+c; here b and c are operands, and the operation is being performed is
‘addition’ by using ‘+’ operator.
Expression:
• An expression is a collection of operands and operators that can be evaluated to a
single value. This is called expression.
Example: 2+6 evaluates to 8 this means 2+6=8 ,4*5=20 this is expression, 48/8=6
this is expression. 6 + (7+6) (i)/(3*j/-k) – 84.27 this is expression.
Classifications of operators:
C is very rich in operators and is sometimes called “the language of operators”. It
has as many as 45 different operators. Some commonly used operators are:
1) Arithmetic operators
2) Assignment operators
3) Relational operators
4) Logical/Boolean operators
5) Increment and decrement operators
Arithmetic Operators:
• Arithmetic operators are used to perform mathematical operations. An
arithmetic expression is made of constant, variable, a combination of both
connected by arithmetic operators.
• The operands acted upon by arithmetic operators must represent numeric values.
• The remainder operators (%) requires that both operands be integers and the
second operand be nonzero. 12%0---------wrong
• Example: 5%4.5 wrong
• 5% 4 correct
• Similarly the division operator (/) requires the second operand be non zero. 5/0---
----wrong.

• Division of one integer quantity by another is referred to as integer division. This


always results in a truncated quotient.( the decimal portion of the quotient will
be dropped. 7/2= 3.5 but decimal portion will be dropped and answer is 3.
• On the other hand , if a division is carried out with two floating point numbers or
the result will be floating point. Example if result 2 then will take 2.00.
• Examples: now suppose that I and j are integer variables whose values are 7 and
2 respectively.
• Expression value
• i+j =9
i-j =5
i*j =14
i/j =3
i%j =1

See book page 92 for further examples.


Order of precedence
• the basic assignment operator is =. This is used
to assign value of an expression to an variable
(identifier).
• The assignment operator has right to left associativity for example
• Expression meaning
• A=5 assign integer value 5 to variable a.
• For further example see book page 95.
• here a=b=c=d=1 -------meaning first the value 1 is assigned to d , then d assigned
to c, then c assigned to b and finally b is assigned to a.

• Another example: int I;


• double f;
• f=i=3.5 ;--------------meaning assigns 3.5 to f and 3 to i.
For example i=i*5 can be written as i*=5 see book page 96 for exercise practice.
Relational operators:
• Relational operators are used to compare two numeric operands . The operands
can be variables , constant or expression that ultimately get evaluated to a
numerical value . Since character can be represented as integers using ASCII code,
therefore, operands to be compared can be character as well.
• All relational operator is made of two arithmetic expressions connected by a
relational operator . It returns 0 when answer is false and 1 when answer is true.
Logical /Boolean operators
• The concept of logical operators is simple. They allow a program to
make a decision based on multiple conditions. Each operand is
considered a condition that can be evaluated to a true or false value.
Operator Operation example
&& Logical AND 1 if a=b && c=d; else 0
Logical OR 1 if a=b; c>d; else 0
II
! Logical NOT 1 if !a; else 0
See book page 99, 100 & 101
Conversion of data type
• Suppose that an arithmetic expression such as I + j has both value and a data
type. For example , if both I and j have type int then the expression i+j also
has type int. but if I has int and j has float so int would be automatically
converted to a float.

You might also like