Hour 8 Using Conditional Operators

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

HOUR 8

Using Conditional
Operators
Measuring Data Sizes
The sizeof Operator
You can measure the data type size by
using the sizeof operator provided by C.

The general form of the sizeof operator is:


sizeof (expression)
where:
expression - is the data type or
variable whose size is
measured by the sizeof operator.
Measuring Data Sizes
The sizeof Operator
 The sizeof operator evaluates the size,
in bytes, of its operand.
 The operand of the sizeof operator may
be a C language keyword naming a
data type (such as int, char, or float),
or it may be an expression which refers
to a data type whose size can be
determined (such as a constant or the
name of a variable).
Measuring Data Sizes
The sizeof Operator
 The parentheses are optional in the
general form of the operator. If the
expression is not a C keyword for a data
type, the parentheses can be discarded.

Example:
size = sizeof(int);
Measuring Data Sizes
Measuring Data Sizes
Everything Is Logical
The Logical Operators
There are three logical operators in the C
language:
&& The logical AND operator
|| The logical OR operator
! The logical NEGATION operator
Everything Is Logical
The Logical Operators
 The logical AND and logical OR
operators, are binary operators; that
is, they both take two operands (one to
the left and one to the right of the
operator).
 The logical negation operator (!) is a
unary operator; that is, it only takes one
operand (the expression to its right).
Everything Is Logical
The Logical AND Operator (&&)
A general format of the logical AND
operator is:

exp1 && exp2

where exp1 and exp2 are two operand


expressions evaluated by the AND
operator.
Everything Is Logical
The Logical AND Operator (&&)
Everything Is Logical
The Logical AND Operator (&&)
Everything Is Logical
The Logical AND Operator (&&)
Everything Is Logical
The Logical OR Operator (||)
A general format of the logical OR operator
is:

exp1 || exp2

where exp1 and exp2 are two operand


expressions evaluated by the OR operator.
Everything Is Logical
The Logical OR Operator (||)
Everything Is Logical
The Logical Negation Operator (!)
The general format of the logical
NEGATION operator is:

! expression

where expression is the operand of the


NEGATION operator.
Everything Is Logical
The Logical Negation Operator (!)
Everything Is Logical
The Logical Negation Operator (!)
Everything Is Logical
The Logical Negation Operator (!)
What Does x?y:z Mean?
The Conditional Operator
The operator ?: is called the conditional
operator, which is the only operator that
takes three operands.

The general form of the conditional


operator is:
x?y:z
where x contains the test condition, and y
and z represent the two possible final
values of the expression.
What Does x?y:z Mean?
The Conditional Operator
x?y:z

If x evaluates to nonzero (logically true),


then y is chosen; otherwise, z is the result
yielded by the conditional
expression.
What Does x?y:z Mean?
The Conditional Operator
Example:
x > 0 ? ‘T’ : ‘F’

The expression evaluates to ‘T’ if the value


of x is greater than 0. Otherwise, the
conditional expression evaluates to the
value ‘F’.

You might also like