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

Programming Language (Assignment 02) Supporting Material

The document explains the conditional (ternary) operator in C, which simplifies if-else statements into a single line. It includes syntax, working principles, and examples for finding the maximum of two numbers and determining if a number is odd or even. The conditional operator is noted for its higher performance compared to traditional if-else statements.

Uploaded by

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

Programming Language (Assignment 02) Supporting Material

The document explains the conditional (ternary) operator in C, which simplifies if-else statements into a single line. It includes syntax, working principles, and examples for finding the maximum of two numbers and determining if a number is odd or even. The conditional operator is noted for its higher performance compared to traditional if-else statements.

Uploaded by

marrium sajjad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like