Conditional Operator Notes - C Language
Conditional operator is a special operator which is also called ternary operator. It performs testing and result of testing, in one line.
General form of using it is as follows:
Conditional Expression ? Exp1 : Exp 2 ;
Here, at first. conditional expression is evaluated. If the condition is true Exp 1 is evaluated otherwise Exp 2 is evaluated.
The working of Conditional Operator is similar to If-Else.
For Example:
r = (a > b) ? a : b ;
Above expression can also be solved with the help of If- Else in following way:
if(a > b)
{
r = a;
}
else
{
r = b;
}
Conditional Operator Flowchart :
Let us understand Conditional operator with an example program
/* A program to show the working of Conditional Operator */
#include<stdio.h>
int main()
{
int r,a,b;
printf("Enter two no.s \n");
scanf("%d \t %d", &a,&b);
r= (a>b)?a:b;
printf ("%d is greater",r);
return 0;
}
After Execution, following will be the output:
0 Comments