1. BS (HONS) COMPUTER SCIENCE
SUBMITTED BY : KHADIJA TAHIRA
ROLL NO. : 043 (A-1)
SUBJECT : PF
ASSIGNMENT TOPIC : TERNARY OPERATOR
SUBMITTED TO : TAYYAB SULTANA
2. TERNARY OPERATOR
Programmers use ternary operators for decision making in place of conditional
statements if and else. The ternary operator is an operator that takes three
arguments. The ternary operator is an operator that takes three arguments. The first
argument is a comparison argument, the second is the result upon a true comparison, and the
third is the result upon a false comparison.It can be used to replace multiple lines of code with
a single line.
FLOWCHART OF TERNARY OPERATOR
WORKING OF TERNARY OPERATOR
In ternary operator, first Condition is evaluated to either TRUE or FALSE. If the result of
Condition is TRUE then True Expression is executed. If the result of Condition is FALSE then
False Expression is executed. Therefore, result of conditional operator is the result of
whichever expression is evaluated the True Expression or False Expression. Only one of the
expression is evaluated in a conditional operator and never both. Given flowchart illustrates
the operation of conditional operator.
4. PURPOSE OF TERNARY OPERATOR
The ternary operator is an operator that exists in some programming languages, which takes
three operands rather than the typical one or two that most operators use. It provides a way to
shorten a simple if else block. With this type of comparison, you can shorten the code using
the ternary operator. One of the purpose is to summarize statements in a single line.
PROGRAM
PROGRAM EXPLANATION
In this program, I declare a variable by y and take value from the user.I use system(“CLS”) for
clear previous screen. Put condition y%4==0 and expressions. First expression is for true
statement ( i.e o) and Second one is for false statement ( i.e 1 ).After this, I use if else . Enter
5. condition in if is y==0 and statement is leap year and in else statement is not a leap year. Now
I discuss about working, If the condition is true it executes if statement and if it is false then it
executes false statement. In the given program, user enter 2020 year, user checks the condition.
It is true that’s why output is Leap year.
PROGRAM
PROGRAM EXPLANATION
In the example above I use one variable age and assign value 19, to test whether age is
greater than 16 (using the > operator). As age is 19, we know that 19 is greater than 16, user
cout to the screen that “you are a teenager".