In C language, the conditional operator is also known as the ternary operator. The conditional statement is a statement that makes decisions based on the output of an expression. It is represented by two symbols: '?' and ':'.
Since the conditional operator operates on three operands, it is also called a ternary operator.
The behavior of the conditional operator is similar to that of the 'if-else' statement because the 'if-else' statement is also a decision-making statement.
The syntax of the conditional operator is as follows:
Expression1 ? expression2 : expression3;
👇 Click to receive 👇
👉 C Language Knowledge Resource Collection
The above syntax is illustrated as follows:
-
In the above syntax, expression1 is a boolean condition that can be either true or false.
-
If the result of expression1 is true, expression2 is executed.
-
Expression2 is considered true only when it returns a non-zero value.
-
If expression1 returns false, expression3 is executed.
-
Expression3 is considered false only when it returns a zero value.
Let’s understand the ternary or conditional operator through an example
#include <stdio.h> int main() { int age; // Variable declaration printf("Please enter your age:"); scanf("%d", &age); // Get user input for age (age >= 18) ? (printf("Eligible to vote")) : (printf("Not eligible to vote")); // Conditional operator return 0; }
In the above code, we get the user’s input for age. After obtaining the input, we apply the conditional operator. In this condition, we check the user’s age. If the user’s age is greater than or equal to 18, statement 1 is executed, which is printf("Eligible to vote")
; otherwise, statement 2, which is printf("Not eligible to vote")
, is executed.
Let’s observe the output of the above program.
If the user age provided is less than 18, the output will be:
Please enter your age: 15Not eligible to vote
If the user age provided is greater than 18, the output will be:
Please enter your age: 20Eligible to vote
From the above two outputs, we can observe that if the condition is true, statement 1 is executed; otherwise, statement 2 is executed.
So far, we have seen how the conditional operator checks conditions and executes statements based on those conditions. Now, we will see how the conditional operator can be used for variable assignment.
Let’s understand this situation through an example.
#include <stdio.h> int main() { int a=5, b; // Variable declaration b = ((a == 5) ? (3) : (2)); // Conditional operator printf("The value of variable 'b' is: %d", b); return 0; }
In the above code, we declare two variables a
and b
, assigning 5 to variable a
. After declaration, we use the conditional operator to assign a value to variable b
. If the value of variable a
equals 5, then variable b
is assigned 3; otherwise, it is assigned 2.
The value of variable 'b' is: 3
The above output shows that the value of variable b
is 3 because the value of variable a
is equal to 5.
As we know, the conditional operator behaves similarly to the if-else
statement, but there are some differences between them. Let’s look at their differences.
-
The conditional operator is a single programming statement, while the
if-else
statement is a programming block that contains multiple statements within braces.
-
The conditional operator can also be used for variable assignment, while the
if-else
statement cannot be used for assignment.
-
When it comes to multiple statements, the conditional operator is not suitable for executing statements, while the
if-else
statement is more appropriate for executing multiple statements.
-
Nesting ternary operators is more complex and harder to debug, while nested
if-else
statements are easier to read and maintain.
Programmer Technical Exchange Group
Scan the code to join the group, remember to note: city, nickname, and technical direction.