Conditional Operator in C (Ternary Operator in C)
Conditional Operator in C (Ternary Operator in C)
Introduction
Conditional Operator can be used as alternative for if else Conditional statement.
Lets Understand how if else statement works.
if(Condition)
{
Block to be executed
if Condition becomes True;
}
else
{
Block to be executed
if Condition becomes False;
}
if Condition becomes true, First Block gets execute whereas if Condition becomes false, else block gets execute.
Conditional Operator (?:) works exactly same. Lets know how conditional operator (?:) in C works in detail.
Syntax :
Expression1 ? Expression2 : Expression3 ;
Expression1 gets execute first, if it becomes true (Returns Non Zero) Expression 2 gets execute and becomes value of entire expression.
If Expression1 becomes false (Returns Zero) Expression 3 gets execute and becomes value of entire expression.
Here, Expression2 is replacing First Block in if else statement whereas Expression3 is replacing else block.
Conditional Operator has Three Operands in entire expression, so it is also referred as Ternary Operator.
Examples
C Program to check whether given number is even or odd.
{
int num;
printf("\n\t\t ENTER NUMBER : ");
scanf("%d",&num);
num%2==0 ? printf("%d IS EVEN",num) : printf("%d IS ODD",num);
return 0;
}
Here num%2==0 is Expression1 which gets execute first. if it becomes true, First printf() statement before colon (:) gets execute and will display number is even, and if Expression1 becomes false, second printf() statement after colon (:) gets execute and will display number is odd.
In this example Conditional Operator don't have Left Hand Side variable. Sometimes situation may occur where value of entire expression need to be stored in Left Hand Side variable. This can be done in following way.
--------------------------------------------------------------
C Program to find Largest of Two Numbers.
{
int n1,n2,large;
printf("ENTER TWO NUMBERS : ");
scanf("%d%d",&n1,&n2);
large=(n1>n2) ? n1 : n2 ;
printf("LARGEST NUMBER : %d",large);
return 0;
}
Here, (n1>n2) is Expression1. if it becomes true n1 gets assign to large and if it becomes false n2 gets assign to large.
In other words, if (n1>n2) becomes true, n1 become the value of entire expression and if (n1>n2) becomes false, n2 become value of entire expression.
Nested Conditional Operator
It is also possible to have Nested Conditional Operator in C. lets know how to use nested conditional operator with simple example.
C Program to find Largest of Three Numbers.
int main()
{
int n1,n2,n3,large;
printf("\n\t\t ENTER THREE NUMBERS : ");
scanf("%d%d%d",&n1,&n2,&n3);
large=(n1>n2) ? ((n1>n3) ? n1 : n3) : ((n2>n3) ? n2 : n3) ;
printf("\n\t\t LARGEST NUMBER : %d",large);
return 0;
}
Here, (n1>n2) i.e. Expression1 gets execute first, if Expression1 becomes true, Expression2 i.e. nested Conditional operator comparing n1 and n3 gets execute . Whereas if Expression1 become false, Expression3 i.e. nested Conditional operator comparing n2 and n3 gets execute.
======== End of Article========
Thanks and Regards
Prof. Shivling G. Swami.
Head of Computer Science and Engineering,
Sant Gajanan Maharaj College of Engineering, Mahagaon.
Whats app and Contact Number : 99 22 970 696
Click here to view my You Tube Profile
Comments
Post a Comment