0% found this document useful (0 votes)
21 views31 pages

Notes 231212 151401

The document discusses different types of operators used in C programming such as arithmetic, relational, logical, assignment, and conditional operators. It provides examples of each operator and how they are used to evaluate expressions. Key points covered include operator precedence, unary vs binary operators, shorthand assignment operators, and using operators in decision making and loops.

Uploaded by

m-868020
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)
21 views31 pages

Notes 231212 151401

The document discusses different types of operators used in C programming such as arithmetic, relational, logical, assignment, and conditional operators. It provides examples of each operator and how they are used to evaluate expressions. Key points covered include operator precedence, unary vs binary operators, shorthand assignment operators, and using operators in decision making and loops.

Uploaded by

m-868020
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/ 31

PROBLEM

SOLVING
IMPLEMENTATION OF OPERATORS AND
EXPRESSION IN PROGRAMMING
COURSE OUTCOMES

• Understand C expression concept – operators, precedence &


associativity and statements.
• Capable to identify usage of each operators and expressions
OPERATOR VS OPERAND

x-y
Operand Operand
(x) (y)

Operator
(-)
DEFINITION OF OPERATORS & OPERANDS

OPERATOR
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions

OPERANDS
• An object on which an operation is performed; it
receives an operator’s action
• Can consist of constants, variables and expression
OPERATORS

• The commonly used operators include:


• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Conditional operators
ARITHMETIC OPERATORS

The following table shows all the arithmetic operators supported by


C language. Assume variable x holds 20 and variable y holds 10 then -

Operator Description Example


+ Adds two operands x + y = 30
- Subtracts second operand from the first x – y = 10
* Multiplies both operands x * y = 200 Binary operators
/ Divides numerator by de-numerator x/y = 2
% Modulus Operators and remainder of after x%y=0
an integer division
++ Increment operator increases the integer x++ = 21
value by one
Unary operators
-- Decrement operator decreases the integer x-- = 19
value by one
ARITHMETIC OPERATORS | UNARY OPERATORS

• Operates on, or affects, a single operand.


Increment and decrement
operators
In C
++ = Increment operator
-- = Decrement operator
…UNARY OPERATORS | POSTFIX VS. PREFIX

PREFIX
When ++ is used as PREFIX (like ++x), ++x
will increment the value of x and then
return it.

POSTFIX
When ++ is used as POSTFIX (like x++),
operator will return the value of operand
first and then only increment it.
EXAMPLE PROGRAM | PREFIX VS POSTFIX

Output
?
BINARY OPERATOR

Operation related to numerical value (integer and float).

Type of operation Example


2 constants 10 + 20;
A variable with a constants x * 10;
2 variables x / y;
BINARY OPERATOR | ORDER OF PRECEDENCE

ARITHMETIC OPERATION PRIORITY


High Priority
() Left to Right
++, -- Left to Right
*, /, % Left to Right
+, - Left to Right
BINARY OPERATOR | ORDER OF PRECEDENCE

OPERATOR OPERATION ORDER OF PRECENDENCE


() Parentheses HIGH.
If nested parentheses, inner
expression is evaluated first
*, /, % Multiplication, Second to be evaluated. If the
division, operators all have the same
modulus precedence, left to right
associatively.
+, - Addition, Last to be evaluated. If the
subtraction operators all have the same
precedence, left-to-right
associatively.
BINARY OPERATOR | PRACTICE

x = 10, y = 20, z = 30

1) x * y + z = ?
2) X * (y + z) = ?

First, calculate manually, later, write a


program for the above operations. Same
answer should be obtained
EXAMPLE PROGRAM | BINARY OPERATORS
OPERATORS

Output
ASSIGNMENT OPERATORS

Different ways of using Assignment Operator

1) Assignment Operator used to Assign Value


2) Assignment Operator used to Type Cast
3) Assignment Operator in If Statement
…ASSIGNMENT OPERATORS : ASSIGN VALUE

1: Assignment Operator used to Assign


• Value
The most common assignment operator is =.
• This operator assigns the value in right side to the left side. For
example:

var = 5; // 5 is assigned to ‘var’


a = c; // Value of c is assigned to ‘a’
5 = c; // Error! 5 is a constant
Eg program:
55 is
assigned to a
variable
‘value’
…SHORTHAND ASSIGNMENT OPERATORS

Operator Name of the Example Equivalent


Symbol Operator Construct
+= Addition x +=4; x = x + 4;
assignment
-= Subtraction x -=4; x = x – 4;
assignment
*= Multiplication x *=4; x = x* 4;
assignment
/= Division x /=4; x = x / 4;
assignment
%= Remainder x %=4; x = x % 4;
assignment
…SHORTHAND ASSIGNMENT OPERATORS |
EXERCISE

Output?
…ASSIGNMENT OPERATORS : TYPE CASTING

• Assignment operator can type cast higher values to


lower values.
• It can also cast lower values to higher values

• Casting is to convert data from one type to another.


• Type casting may be used to force an expression to be
a specific data type.
• Type casting takes the general forms
(type) expression
…ASSIGNMENT OPERATORS : TYPE CASTING

Output?
…ASSIGNMENT OPERATORS : TYPE CASTING

1
Output?

Output?
RELATIONAL OPERATORS

• Relational operators checks relationship between two


operands.
• If the relation is true, it returns value 1 and if the relation is
false, it returns value 0. For example:

x>y
• Here, > is a relational operator. If x is greater than y,
x>y returns 1 if not then, it returns 0.
• Relational operators are used in decision making and loops
in C programming.
RELATIONAL OPERATORS

• Example Flow Chart

Tru Fals
e e
…RELATIONAL OPERATORS

Operator Meaning of Operator Example


== Equal to 5==3 returns false (0)
> Greater than 5>3 returns true (1)
< Less than 5<3 returns false (0)
!= Not equal to 5!=3 returns true (1)
>= Greater than or equal to 5>=3 returns true (1)
<= Less than or equal to 5<=3 returns false (0)
…RELATIONAL OPERATORS

if (this condition is TRUE)


{
execute this statement
}

if (this condition is TRUE)


{
execute this statement
}
else
{
execute this statement instead
}
LOGICAL OPERATORS

• Logical operators are used to combine expressions containing


relation operators.
• In C, there are 3 logical operators:

Operator Meaning of Operator Example

&& Logical AND If x = 5 and y = 2 then,


((x==5) && (y>5 )) returns FALSE

|| Logical OR If x = 5 and y = 2 then,


((x==5) || (y>5)) returns TRUE

! Logical NOT If c = 5 then, !(c==5) returns FALSE


RELATIONAL AND LOGICAL OPERATORS

int a = 10, b = 50;


char ch1 = ‘A’;
float f = 9.5;

Relational + Logical Expression Value


a+f<=b 1 (True)
a < b && ch1==‘A’ 1 (True)
b – f < 5.0 || ch1!=‘A’ 0 (False)
CONDITIONAL OPERATORS

• Conditional operator takes three operands and consists of two symbols


? and :
• Conditional operators are used for decision making in C.
• Used to replace if-else logic in some situations
• Format:

ConditionalExpression ? T-expression : F-expression

If condition is TRUE FALSE


CONDITIONAL OPERATORS

For
example:
c=(c >0) ?10:-10

If c is greater than 0, value of c will be 10 but, if c is less


than 0, value of c will be -10.
EXAMPLE | SIMPLE CASE STUDY

This system will calculate amount of bonus that the


employees will get. If the employee’s years of work is
more than 5 years, they will get RM1000, otherwise they
will get RM500.

Using if-else statement


• Input? Process? Output?
• Flow chart?
• Coding?
THANK YOU
31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy