SlideShare a Scribd company logo
1 of 6
BS (HONS) COMPUTER SCIENCE
SUBMITTED BY : KHADIJA TAHIRA
ROLL NO. : 043 (A-1)
SUBJECT : PF
ASSIGNMENT TOPIC : TERNARY OPERATOR
SUBMITTED TO : TAYYAB SULTANA
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.
SYNTAX OF TERNARY OPERATOR
Variable= condition ? expression 1: expression 2;
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
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".
#include <iostream>
using namespace std;
int main()
{
cout<<“HOME IS OUR SAFE PLACE!”;
}

More Related Content

What's hot (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Loops in c
Loops in cLoops in c
Loops in c
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Interface
InterfaceInterface
Interface
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Array ppt
Array pptArray ppt
Array ppt
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Similar to Ternary operator

CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selectionHamad Odhabi
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 Bdplunkett
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressionsvinay arora
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)Muhammad Hammad Waseem
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Syed Farjad Zia Zaidi
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statementsİbrahim Kürce
 
Chapter 3 branching v4
Chapter 3 branching v4Chapter 3 branching v4
Chapter 3 branching v4Sunarto Quek
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrayssshhzap
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statementFarshidKhan
 

Similar to Ternary operator (20)

CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
Ch04
Ch04Ch04
Ch04
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
Chapter 3 branching v4
Chapter 3 branching v4Chapter 3 branching v4
Chapter 3 branching v4
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
Java script session 4
Java script session 4Java script session 4
Java script session 4
 
Lecture 07.pptx
Lecture 07.pptxLecture 07.pptx
Lecture 07.pptx
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 

More from Lahore Garrison University (20)

Tree in discrete structure
Tree in discrete structureTree in discrete structure
Tree in discrete structure
 
Sigmund freud
Sigmund freudSigmund freud
Sigmund freud
 
Application of de
Application of deApplication of de
Application of de
 
Firm practice management
Firm practice managementFirm practice management
Firm practice management
 
Mapping functions
Mapping functionsMapping functions
Mapping functions
 
Project dld
Project dldProject dld
Project dld
 
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
 
Ak
AkAk
Ak
 
Bussines plan (maira)
Bussines plan (maira)Bussines plan (maira)
Bussines plan (maira)
 
8. file handling
8. file handling8. file handling
8. file handling
 
Project# 1
Project# 1Project# 1
Project# 1
 
Shares &amp; stock market
Shares &amp; stock marketShares &amp; stock market
Shares &amp; stock market
 
Ozone layer research paper (1)
Ozone layer research paper (1)Ozone layer research paper (1)
Ozone layer research paper (1)
 
Introduction to information and communication technology
Introduction to information and communication technologyIntroduction to information and communication technology
Introduction to information and communication technology
 
Ict
IctIct
Ict
 
Forty rulesoflove review writing
Forty rulesoflove review writingForty rulesoflove review writing
Forty rulesoflove review writing
 
Semester 1 exercise for punctuation mark
Semester 1 exercise for punctuation markSemester 1 exercise for punctuation mark
Semester 1 exercise for punctuation mark
 
Scan apr 8, 2020 (1)
Scan apr 8, 2020 (1)Scan apr 8, 2020 (1)
Scan apr 8, 2020 (1)
 
Reading skill
Reading skill Reading skill
Reading skill
 
Quiz
Quiz Quiz
Quiz
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Ternary operator

  • 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.
  • 3. SYNTAX OF TERNARY OPERATOR Variable= condition ? expression 1: expression 2;
  • 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".
  • 6. #include <iostream> using namespace std; int main() { cout<<“HOME IS OUR SAFE PLACE!”; }