SlideShare a Scribd company logo
The Conditional Operators Intro to Programming
MUHAMMAD HAMMAD WASEEM 1
Conditional Operators
The conditional operators ? and : are sometimes called ternary operators since they take
three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
expression 1 ? expression 2 : expression 3
What this expression says is: “if expression 1 is true (that is, if its value is non-zero), then the
value returned will be expression 2, otherwise the value returned will be expression 3”.
Let us understand this with the help of a few examples:
a. int x, y ;
scanf ( "%d", &x ) ;
y = ( x > 5 ? 3 : 4 ) ;
This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.
The equivalent if statement will be,
if ( x > 5 )
y = 3 ;
else
y = 4 ;
b. char a ;
int y ;
scanf ( "%c", &a ) ;
y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;
Here 1 would be assigned to y if a>=65&&a<=90 evaluates to true, otherwise 0 would be
assigned.
The following points may be noted about the conditional operators:
1. It’s not necessary that the conditional operators should be used only in arithmetic statements. This
is illustrated in the following examples:
Ex.: int i;
scanf ( "%d", &i ) ;
(i == 1 ? printf("Amit") : printf ("All and sundry"));
Ex.: char a = 'z';
printf ( "%c" , ( a >= 'a' ? a : '!' ) ) ;
2. The conditional operators can be nested as shown below.
int big, a, b, c ;
big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;
3. Check out the following conditional expression:
a > b ? g = a : g = b ;
This will give you an error ‘Lvalue Required’. The error can be overcome by enclosing the
statement in the : part within a pair of parenthesis. This is shown below:
a > b ? g = a : ( g = b ) ;
In absence of parentheses the compiler believes that b is being assigned to the result of the
expression to the left of second =. Hence it reports an error.
The Conditional Operators Intro to Programming
MUHAMMAD HAMMAD WASEEM 2
Limitations of Conditional Operator:
The limitation of the conditional operators is that after the ? or after the : only one C statement
can occur. In practice rarely is this the requirement. Therefore, in serious C programming conditional
operators aren’t as frequently used as the if-else.

More Related Content

PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
Operators and expressions in c language
tanmaymodi4
 

What's hot (20)

PDF
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
PPT
6 operators-in-c
Rohit Shrivastava
 
PPTX
Decision statements in c language
tanmaymodi4
 
PPT
Mesics lecture 4 c operators and experssions
eShikshak
 
PPTX
c++ programming Unit 4 operators
AAKASH KUMAR
 
PPT
C operator and expression
LavanyaManokaran
 
DOCX
C – operators and expressions
Chukka Nikhil Chakravarthy
 
PPTX
Expression and Operartor In C Programming
Kamal Acharya
 
PPTX
Control Flow Statements
Tarun Sharma
 
DOCX
C Programming
Raj vardhan
 
PDF
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
PDF
Operators in c programming
savitamhaske
 
PPTX
Increment and Decrement operators in C++
Neeru Mittal
 
PPTX
Decision making statements in C programming
Rabin BK
 
PPT
Operator & Expression in c++
bajiajugal
 
PPTX
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
PPTX
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
PPT
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
Operators and expressions in C++
Neeru Mittal
 
6 operators-in-c
Rohit Shrivastava
 
Decision statements in c language
tanmaymodi4
 
Mesics lecture 4 c operators and experssions
eShikshak
 
c++ programming Unit 4 operators
AAKASH KUMAR
 
C operator and expression
LavanyaManokaran
 
C – operators and expressions
Chukka Nikhil Chakravarthy
 
Expression and Operartor In C Programming
Kamal Acharya
 
Control Flow Statements
Tarun Sharma
 
C Programming
Raj vardhan
 
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Operators in c programming
savitamhaske
 
Increment and Decrement operators in C++
Neeru Mittal
 
Decision making statements in C programming
Rabin BK
 
Operator & Expression in c++
bajiajugal
 
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
Ad

Similar to [ITP - Lecture 09] Conditional Operator in C/C++ (20)

PDF
ICP - Lecture 7 and 8
Hassaan Rahman
 
PPTX
chapter 3enejngewngjnjnfjndnennwnnefwf.pptx
TriggeredZulkar
 
PDF
Learning C programming - from lynxbee.com
Green Ecosystem
 
PPT
Decisions in C or If condition
yarkhosh
 
PDF
Loop's definition and practical code in C programming
DharmaKumariBhandari
 
PPTX
Claguage 110226222227-phpapp02
CIMAP
 
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
PDF
3 flow
suresh rathod
 
PDF
3 flow
suresh rathod
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PPTX
What is c
Nitesh Saitwal
 
PPTX
C programming
Xad Kuain
 
PPTX
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PPT
Control statments in c
CGC Technical campus,Mohali
 
PPTX
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
rakshithatan
 
DOCX
Programming Fundamentals lecture 7
REHAN IJAZ
 
DOC
C fundamental
Selvam Edwin
 
PPTX
Decision Making and Branching
Munazza-Mah-Jabeen
 
ICP - Lecture 7 and 8
Hassaan Rahman
 
chapter 3enejngewngjnjnfjndnennwnnefwf.pptx
TriggeredZulkar
 
Learning C programming - from lynxbee.com
Green Ecosystem
 
Decisions in C or If condition
yarkhosh
 
Loop's definition and practical code in C programming
DharmaKumariBhandari
 
Claguage 110226222227-phpapp02
CIMAP
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
What is c
Nitesh Saitwal
 
C programming
Xad Kuain
 
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Control statments in c
CGC Technical campus,Mohali
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
rakshithatan
 
Programming Fundamentals lecture 7
REHAN IJAZ
 
C fundamental
Selvam Edwin
 
Decision Making and Branching
Munazza-Mah-Jabeen
 
Ad

More from Muhammad Hammad Waseem (20)

PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 03] Programming Paradigms
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 02] Why do we need OOP
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
PPTX
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
[OOP - Lec 03] Programming Paradigms
Muhammad Hammad Waseem
 
[OOP - Lec 02] Why do we need OOP
Muhammad Hammad Waseem
 
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 

Recently uploaded (20)

PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Understanding operators in c language.pptx
auteharshil95
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 

[ITP - Lecture 09] Conditional Operator in C/C++

  • 1. The Conditional Operators Intro to Programming MUHAMMAD HAMMAD WASEEM 1 Conditional Operators The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is, expression 1 ? expression 2 : expression 3 What this expression says is: “if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3”. Let us understand this with the help of a few examples: a. int x, y ; scanf ( "%d", &x ) ; y = ( x > 5 ? 3 : 4 ) ; This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. The equivalent if statement will be, if ( x > 5 ) y = 3 ; else y = 4 ; b. char a ; int y ; scanf ( "%c", &a ) ; y = ( a >= 65 && a <= 90 ? 1 : 0 ) ; Here 1 would be assigned to y if a>=65&&a<=90 evaluates to true, otherwise 0 would be assigned. The following points may be noted about the conditional operators: 1. It’s not necessary that the conditional operators should be used only in arithmetic statements. This is illustrated in the following examples: Ex.: int i; scanf ( "%d", &i ) ; (i == 1 ? printf("Amit") : printf ("All and sundry")); Ex.: char a = 'z'; printf ( "%c" , ( a >= 'a' ? a : '!' ) ) ; 2. The conditional operators can be nested as shown below. int big, a, b, c ; big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ; 3. Check out the following conditional expression: a > b ? g = a : g = b ; This will give you an error ‘Lvalue Required’. The error can be overcome by enclosing the statement in the : part within a pair of parenthesis. This is shown below: a > b ? g = a : ( g = b ) ; In absence of parentheses the compiler believes that b is being assigned to the result of the expression to the left of second =. Hence it reports an error.
  • 2. The Conditional Operators Intro to Programming MUHAMMAD HAMMAD WASEEM 2 Limitations of Conditional Operator: The limitation of the conditional operators is that after the ? or after the : only one C statement can occur. In practice rarely is this the requirement. Therefore, in serious C programming conditional operators aren’t as frequently used as the if-else.