Full Stack Developer
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements that depend upon the output of the expression. As a conditional operator works on three operands, it is also known as the ternary operator.
The operands may be an expression, constants, or variables. It starts with a condition, hence it is called a conditional operator. Conditional operators return one value if the condition is true and returns another value if the condition is false.
expression1? expression2: expression3;
or for simplicity, we write it as
condition ? true-statement : false-statement;
The expression1 is evaluated, it is treated as a logical condition. If the result is non-zero then expression2 will be evaluated otherwise expression3 will be evaluated. The value after evaluation of expression2 or expression3 is the final result.
The conditional operator in C works similarly to the conditional control statement if-else. Hence every code written using a conditional operator can also be written using if-else. When compared with if-else, conditional operator performance is high.
if(expression1)
{
expression2;
}
else
{
expression3;
}
We will use these operators to understand the working of conditional operators in C with implementation.
Code:
#include <stdio.h>
int main()
{
int p = 20 , q = 20 , r = 30 , outcome ;
outcome = ( p == q ) && ( r > q ) ;
printf ( " The result of ( p == q ) && ( r > q ) is %d \n " , outcome ) ;
outcome = ( p == q ) && ( r < q ) ;
printf ( " The result of ( p == q ) && ( r < q ) is %d \n " , outcome ) ;
outcome = ( p == q ) || ( r < q ) ;
printf ( " The result of ( p == q ) || ( r < q ) is %d \n " , outcome ) ;
outcome = ( p != q ) || ( r < q ) ;
printf ( " The result of ( p != q ) || ( r < q ) is %d \n " , outcome ) ;
outcome = ! ( p != q ) ;
printf ( " The result of ! ( p == q ) is %d \n " , outcome ) ;
outcome = ! ( p == q ) ;
printf ( " The result of ! ( p == q ) is %d \n " , outcome ) ;
return 0 ;
}
Output:
In the above code, we have declared 4 integers p, q, r, and outcome in which the outcome will store the outcome of all the applied conditions on the remaining 3 integer variables. Then one by one we applied these &&, ||, and! = operations on the declared variables. So the values will be compared as per the mentioned code and results will be stored in outcome integer.
Code:
#include <stdio.h>
int main()
{
int marks_obtained ;
printf ( " Please enter the obtained marks : " ) ;
scanf ( " %d " , &marks_obtained ) ;
if ( marks_obtained >= 36 )
printf ( " \n Congratulations!! You Passed " ) ;
else
printf ( " \n You Failed " ) ;
return 0 ;
}
Input:
Output:
Code:
#include <stdio.h>
int main()
{
int marks_obtained ;
printf ( " Please enter the marks obtained : " ) ;
scanf ( " %d " , &marks_obtained ) ;
puts ( marks_obtained >= 36 ? " Congratulations!! You Passed" : " You Failed " ) ;
return 0 ;
}
Input:
Output:
If you compare the last two codes they are performing the same operations with the same integers and conditions. The only difference we can see in the last second is using the if-else condition to check the condition of the mark and then displaying the output whereas, in the last code, we are using conditional operators to perform the same operation with the same integers and obtained marks value. Therefore, to display why conditional operators are used, you can see from the last two codes that using a conditional operator instead of using if-else can save a few lines of code. Apart from this, it is more efficient as if one condition is already true then the compiler won’t even go to the rest of the conditions which will save compilation and execution time both. In terms of memory, a conditional operator will also save space.
Conditional operators in C play a crucial role as it saves a lot of time. Instead of putting an if-else loop, we can use these operators because we only have to write two conditions in one line which saves time and space both by making efficient code.