SlideShare a Scribd company logo
1 of 10
Download to read offline
Cconditional
Operators
Said Msihullah Hashimi, M.Sc. NTM
INTRODUCTION
○ The conditional operator is the one operator in
C that is considered a ternary operator,
specifically because it requires three operands.
○ Binary operators require two operands, and
unary operators require one operand. So, the
conditional operator is not THE ternary
operator. It is an operator that just happens to
be ternary.
○ The conditional operator is a shorthand for a
specific if-else control structure scenario.
2
Syntax
“
4
○ The basic syntax of a Ternary Operator in C Programming is as
shown below:
expression ? statement1: statement2
○ From the above syntax, If the given test condition is true then it
will return statement1 and if the condition is false then statement2
is returned.
Slide to see how it works.
5
Example
6
/* Ternary Operator in C Example */
#include<stdio.h>
int main()
{
int age;
printf(" Please Enter your age here: n ");
scanf(" %d ", &age);
(age >= 18) ? printf(" You are eligible to Vote ") : printf(" You are not eligible to Vote ");
return 0;
}
Compare with if-else
7
char *diagnosis;
double temperature;
if (temperature <= 98.6)
{
diagnosis = "well";
}
else
{
diagnosis = "sick";
}
printf("With a temperature of %.1f,
you appear to be
%s.n",temperature, diagnosis);
If-else and
Conditional
Code block
Comparison
char *diagnosis;
double temperature;
diagnosis = (temperature <= 98.6) ?
"well" : "sick";
printf("With a temperature of %.1f, you
appear to be %s.n",
8
Points to
Remember
○ The conditional operator does not take the place of all if-else control
structures. It is designed specifically for the situation where an
expression needs to take on one value if the condition is true, and a
different value of the condition is false.
○ Some coding/style standards consider the conditional operator to be
less readable than the equivalent if-else statement, so check your local
coding/style standard.
○ Although conditional operators can be nested (i.e., another conditional
operator can be used inside the true section between the ? and :,
another conditional operator can be used inside the false section after
the :), this practice leads much less readable code.
○ Nesting of conditional operators should be avoided, to keep the code
readable and maintainable.
○ You may find the conditional operator used in #define macros, because
it facilitates conditional decisions within an expression.
9
THE END
10

More Related Content

What's hot

10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programmingManoj Tyagi
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in CPrabhu Govind
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
types of loops and what is loop
types of loops and what is looptypes of loops and what is loop
types of loops and what is loopwaheed dogar
 
Conditional statement
Conditional statementConditional statement
Conditional statementMaxie Santos
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators pptViraj Shah
 

What's hot (20)

Relational operators
Relational operatorsRelational operators
Relational operators
 
10. switch case
10. switch case10. switch case
10. switch case
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Clanguage
ClanguageClanguage
Clanguage
 
Branching in C
Branching in CBranching in C
Branching in C
 
Nested loops
Nested loopsNested loops
Nested loops
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
types of loops and what is loop
types of loops and what is looptypes of loops and what is loop
types of loops and what is loop
 
Conditional statement
Conditional statementConditional statement
Conditional statement
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Type conversion
Type conversionType conversion
Type conversion
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 

Similar to Conditional operators

CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selectionHamad Odhabi
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopPriyom Majumder
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Python Programming Module 3 (2).pdf
Python Programming Module 3 (2).pdfPython Programming Module 3 (2).pdf
Python Programming Module 3 (2).pdfThanmayee S
 

Similar to Conditional operators (20)

C programming session3
C programming  session3C programming  session3
C programming session3
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
C operators
C operatorsC operators
C operators
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Python Programming Module 3 (2).pdf
Python Programming Module 3 (2).pdfPython Programming Module 3 (2).pdf
Python Programming Module 3 (2).pdf
 

More from BU

Network Attached Storage Security
Network Attached Storage Security Network Attached Storage Security
Network Attached Storage Security BU
 
Variables
VariablesVariables
VariablesBU
 
Do while loop
Do while loopDo while loop
Do while loopBU
 
Tarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfTarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfBU
 
Operating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvinOperating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvinBU
 
IP Routing
IP RoutingIP Routing
IP RoutingBU
 
Secure socket layer
Secure socket layerSecure socket layer
Secure socket layerBU
 
Handy back up installation
Handy back up installationHandy back up installation
Handy back up installationBU
 
Ip hiding using proxy
Ip hiding using proxyIp hiding using proxy
Ip hiding using proxyBU
 
Crash plane-backup-solution
Crash plane-backup-solutionCrash plane-backup-solution
Crash plane-backup-solutionBU
 

More from BU (10)

Network Attached Storage Security
Network Attached Storage Security Network Attached Storage Security
Network Attached Storage Security
 
Variables
VariablesVariables
Variables
 
Do while loop
Do while loopDo while loop
Do while loop
 
Tarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfTarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdf
 
Operating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvinOperating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvin
 
IP Routing
IP RoutingIP Routing
IP Routing
 
Secure socket layer
Secure socket layerSecure socket layer
Secure socket layer
 
Handy back up installation
Handy back up installationHandy back up installation
Handy back up installation
 
Ip hiding using proxy
Ip hiding using proxyIp hiding using proxy
Ip hiding using proxy
 
Crash plane-backup-solution
Crash plane-backup-solutionCrash plane-backup-solution
Crash plane-backup-solution
 

Recently uploaded

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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
“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
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
“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...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
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
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

Conditional operators

  • 2. INTRODUCTION ○ The conditional operator is the one operator in C that is considered a ternary operator, specifically because it requires three operands. ○ Binary operators require two operands, and unary operators require one operand. So, the conditional operator is not THE ternary operator. It is an operator that just happens to be ternary. ○ The conditional operator is a shorthand for a specific if-else control structure scenario. 2
  • 4. “ 4 ○ The basic syntax of a Ternary Operator in C Programming is as shown below: expression ? statement1: statement2 ○ From the above syntax, If the given test condition is true then it will return statement1 and if the condition is false then statement2 is returned.
  • 5. Slide to see how it works. 5
  • 6. Example 6 /* Ternary Operator in C Example */ #include<stdio.h> int main() { int age; printf(" Please Enter your age here: n "); scanf(" %d ", &age); (age >= 18) ? printf(" You are eligible to Vote ") : printf(" You are not eligible to Vote "); return 0; }
  • 8. char *diagnosis; double temperature; if (temperature <= 98.6) { diagnosis = "well"; } else { diagnosis = "sick"; } printf("With a temperature of %.1f, you appear to be %s.n",temperature, diagnosis); If-else and Conditional Code block Comparison char *diagnosis; double temperature; diagnosis = (temperature <= 98.6) ? "well" : "sick"; printf("With a temperature of %.1f, you appear to be %s.n", 8
  • 9. Points to Remember ○ The conditional operator does not take the place of all if-else control structures. It is designed specifically for the situation where an expression needs to take on one value if the condition is true, and a different value of the condition is false. ○ Some coding/style standards consider the conditional operator to be less readable than the equivalent if-else statement, so check your local coding/style standard. ○ Although conditional operators can be nested (i.e., another conditional operator can be used inside the true section between the ? and :, another conditional operator can be used inside the false section after the :), this practice leads much less readable code. ○ Nesting of conditional operators should be avoided, to keep the code readable and maintainable. ○ You may find the conditional operator used in #define macros, because it facilitates conditional decisions within an expression. 9