ASSIGNMENT # 02
(Research on the particular topic)
It is similar to the if-else statement. The if-else statement takes more than one
line of the statements, but the conditional operator finishes the same task in a
single statement. The conditional operator in C is also called the ternary operator
because it operates on three operands. The operands may be an expression,
constants or variables. It starts with conditions; hence it is called a conditional
operator. When compared with if-else, conditional operator performance is high.
Syntax:
Expression1 ? expression2 : expression3;
or
condition ? true statement : false statement;
Working of conditional operator:
1. The expression1 is evaluated; it is treated as a logical condition.
2. If the result is non-zero then expression2 will be evaluated otherwise
expression3 will be evaluated.
3. The value after evaluation of expression2 or expression3 is the final result.
Main Body:
Code 1:
Find maximum in the given two numbers using the conditional operator
in C.
We can also try this
program for the finding
of minimum number in
the given two numbers
using the conditional
operator.
OUTPUT:
First the expression, (num1 > num2) is evaluated. Here num1=12.5 and
num2=10.5; so expression (num1>num2) becomes true. Hence num1 is the
maximum number, and it is assigned to the variable max. (Explanation of output).
CODE 2:
Finding number is odd or even using the conditional operator in C.
OUTPUT:
LENGTHY CODES:
MAIN BODY:
The above multiple lines of code can be replaced with:
CODE:3
OUTPUT:
CODE: 4