Conditional Operators in C Language
0 307
Conditional Operators in C Language, such as the Ternary Operator (' ?: ') provide way to make decision based on conditions.
They work similarly like a if-else statement .
If condition is true, first expression is executed. if condition is false second expression is executed.
This functionality makes code more efficient and easy to understand by simplifying the decision making process.
The Conditional Operators is called as Ternary Operator because it operates on three operands. making it different from other operators in C language.
Conditional Operators in C
Example:
#include<stdio.h> int main() { int n = 10; char* output; output = (n%2 == 0 )? "Even" : "odd"; printf("The number is %s \n", output); return 0; }Output:
The number is EvenIn this code, if the number is even the output will be "Even", otherwise the output will be "Odd".
The ternary operator allows us to express decision making logic in a short and straightforward way.
Share:
Comments
Waiting for your comments