[go: up one dir, main page]

0% found this document useful (0 votes)
19 views17 pages

PSPC Lecture 5,6 - Binary and Ternary Operators

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

PSPC Lecture 5,6 - Binary and Ternary Operators

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

BINARY AND TERNARY

OPERATORS
Learning outcomes

 Students will be able to:


 Comprehend and apply various binary operators such
as arithmetic, logical and relational operators
 Write programs for conditional structures using ternary
operators
EXPRESSIONS
 An expression is a combination of variables,
constants (also called as operands) and operators.
 In C every expression evaluates to a value i.e.,
every expression results in some value of a
certain type.
 If want to store the value of the expression we
can assign it to a variable.
 Depending upon the number of operands, an
operator can be classified as unary, binary or
ternary.
BINARY OPERATORS IN C
 Binary operators are operators that work with two operands.
ARITHMETIC OPERATORS

 Used to perform arithmetic operations


 The following symbols are used to perform
basic arithmetic operations:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division (Gives the quotient)
% : Modulus (Gives the remainder)
ARITHMETIC OPERATORS
 Arithmetic
operators have the following order
of precedence:
1) %, *, /
2) +,-
 However, if there are more than one
operators of equal precedence, it will be
evaluated from left to right.
 Example:
a+b+c-d ( ( (a+b) +c)-d)
ASSIGNMENT OPERATORS
 Syntax:
L.H.S = R.H.S
 The R.H.S can be a :
 Variable
 Constant
 Expression
 The L.H.S of an assignment operator can only be a
variable.
 This is because only variables have memory
capacity to store a value.
COMPOUND ASSIGNMENT
OPERATORS
 Assignment operators can also be compounded
assignment operator.
 Example:
 L.H.S += R.H.S actually means, L.H.S = L.H.S+R.H.S
 All assignment operators have equal precedence
 If there are more than one assignment operator, it
is executed from right to left
QUICK EXERCISE
 Predict the output of the following code:
In C, arithmetic
int x=5, y=6,z=8;
operators (like +, -,
x=y=z; *, /, %) have higher
printf(“%d %d %d ”, x,y,z); precedence than
Output: 8 8 8 assignment
operators (=, +=, -
 Predict the output of the following code:
=, etc.), and while
int a = 10, b = 20, c = 5; arithmetic operators
associate left to
a = b += c + 2; right, assignment
printf("%d %d %d", a, b, c); operators associate
right to left.
Output: 27 27 5
RELATIONAL OPERATORS
 Sometimes we might want to compare two
different things in the program
 Relational operators are:
>,<,>=,<=,==,!=
 The relational expression generates the output as
true/false i.e a Logical 1 or a Logical 0
 What if we want to do multiple comparisons in the
same expression?
 Whether a number lies between 10 and 20
LOGICAL OPERATORS
 Consider the previous example
10<x<20
 When we have multiple comparisons in a single expression,
we combine them using a logical operator (&&, ||)
 The operators follow the truth table of a logical AND and
Logical OR
 Thus like relational expressions, the logical expressions also
generate a Logical 0 or 1.
 Any non-zero value is considered as a Logical 1 and a 0 is
considered as a Logical 0.
 If the value of the first expression generates a definite value,
the second expression is not evaluated.
RELATIONAL AND LOGICAL
OPERATORS
 The order of precedence of relational operators are:
 <, <=, >,>=
 ==,!=
 The order precedence of both logical operators are
the same.
 When both relational operators are used in the same
expression,
 Firstly the relational operators are evaluated from left to
right
 Secondly, the logical operators are evaluated from left to
right.
QUICK EXERCISE
 Find the output of the following code:
int x=8,y=4,z,c=-4,d;
d = (x>y) && (c);
z = ( x!=y) || ( c=0 );
printf(“%d %d %d”, d,z,c);

Output:
1 1 -4
QUICK EXERCISE
 Represent the following statements using relational and logical
expressions
1. “ If a person is 18 years or older, they can vote.“
2. “A person can add at the most 5 items to the shopping cart“
3. "A student qualifies for a scholarship if their GPA is at least 3.5
or their extracurricular score is at least 85."
4. "A loan will be approved if the person has a credit score greater
than 700 and a monthly income of at least 50000“
TERNARY OPERATOR
 Also called as conditional operator.
 Syntax:
condition ? expression1 : expression2;
 The condition is an expression that yields a true/false (Logical
0/Logical 1).
 If it yields a true, expression 1 will be executed,
 If it yields a false, expression 2 will be executed.
 Can be combined with assignment operator a follows:
variable = (condition ? expression1: expression2);
QUICK EXERCISE
 Find the output:
int x=3, y=10, z,sum;
z= x>y ? x+4 : y*2;
sum = z+=6;
printf(“%d %d”, z, sum);
Output:
26 26
WRITE PROGRAMS FOR THE
FOLLOWING
 Program to input a two digit number and divide
the larger digit by the smaller digit.

 Program to find if a number is divisible by 6 or not.

 Programto find largest of three numbers. (Hint:


Only two ternary operators must be used)

You might also like