Updated March 31, 2023
Introduction to Conditional Operator in C
If we break these two words then the operator means a symbol that operates on some value while a condition is something that can be applied to the operator to perform some specific operations. The conditional operator has two value and it shows the output value based on the given conditions. If one condition is true then it will show a new and if another condition is true it will show a different value this is how a condition operator works in C. If a condition is true value will be returned it is similar to if-else loop in programming.
Conditional Operator is also known as Ternary operator. Let us have a look at the syntax of declaring a condition operator in C programming :
Condition ? True_value : False_value
Therefore, according to the syntax, we can put our condition where it is written then if that condition holds true it will evaluate to the first statement otherwise it will go to the second condition.
For example :
Expression 1 ? Expression 2 : Expression 3
Expression 1 is the condition part and rests both of the expressions are a statement on which the condition will depend.
How Does Conditional Operators Work in C?
Now let’s see how does conditional operators works in C programming and how to implement these conditions in our C code. But first, we will see types of Conditional operators and their uses:
Operators | Descriptions |
&& | This conditional operator will return a true value if both the given conditions are true. |
|| | This conditional operator will return a true value if either of the given conditions is true. |
! | This conditional operator will return a true value if the given condition is false. |
& | This conditional operator will return a true value if both the given conditions are true and Boolean. |
| | This conditional operator will return a true value if either of the given conditions is true and Boolean. |
^ | This conditional operator will return a true value if both the given conditions are different. |
Examples to Implement Conditional Operator in C
We will use these operators to understand the working of conditional operators in C with implementation.
Example #1
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 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.
Example #2
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:
Example #3
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 exact same integers and condition. The only difference we can see in the last second is using 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 exact 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 condition which will save compilation and execution time both. In terms of memory, a conditional operator will also save space.
Conclusion
conditional operators in C plays a crucial role as it saves a lot of time because 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.
Recommended Articles
This is a guide to Conditional Operator in C. Here we also discuss the introduction and how does conditional operators work in c along with different examples and its code implementation. You may also have a look at the following articles to learn more –