Ternary Operator Conditional operator in C

The conditional operator in C, also known as the ternary operator, is a shorthand method for writing simple if-else statements. It evaluates a condition and returns one of two values based on whether the condition is true or false.

Syntax of Conditional (Ternary) Operator

The syntax of the conditional operator is:

condition ? expression_if_true : expression_if_false;

  • condition: The condition that evaluates to either true or false.
  • expression_if_true: This expression is evaluated and returned if the condition is true.
  • expression_if_false: This expression is evaluated and returned if the condition is false.

How It Works

  • If the condition is true, the expression before the colon (:) is executed.
  • If the condition is false, the expression after the colon is executed.

Example of the Conditional Operator

Let’s look at a real-life example:

#include <stdio.h>

int main() {

    int a = 10, b = 20;

    // Using the conditional operator to find the larger of two numbers

    int max = (a > b) ? a : b;

    printf(“The larger number is: %d\n”, max);  // Output: 20

    return 0;

}

Explanation:

In this example, the condition a > b is evaluated. Since a is not greater than b, the expression after the colon (b) is returned, which is 20.

Practical Usage

The conditional operator is useful for simplifying simple conditional statements and makes the code more concise and readable. It is commonly used for:

  1. Assigning values based on conditions:
    int age = 18;
  2. char *status = (age >= 18) ? “Adult” : “Minor”;
  3. printf(“%s\n”, status);  // Output: Adult
  4. Replacing simple if-else constructs:

    int a = 5, b = 10;
  5. printf(“The smaller number is: %d\n”, (a < b) ? a : b);  // Output: 5

Real-Life Example

In an educational application for managing student grades, you can use the conditional operator to assign grades based on the marks obtained:

#include <stdio.h>

int main() {

    int marks = 85;

    char *grade = (marks >= 90) ? “A+” :

                  (marks >= 80) ? “A” :

                  (marks >= 70) ? “B” :

                  (marks >= 60) ? “C” : “F”;

    printf(“The grade is: %s\n”, grade);  // Output: A

    return 0;

}

Explanation:

Here, we used the conditional operator multiple times to assign grades based on the marks scored. If marks are greater than or equal to 90, the grade is A+. If marks are greater than or equal to 80 but less than 90, the grade is A, and so on.

Conditional Operator vs if-else

While the ternary operator is a concise way of writing conditions, it is best used for simple conditions where readability is not compromised. For complex conditions or multiple statements, it is better to use the traditional if-else structure to make the code easier to understand.

Key Points to Remember

  1. Readability: Use the conditional operator for simple conditions to make your code more concise.
  2. Multiple Conditions: You can nest multiple ternary operations, but be careful, as this can make the code less readable.
  3. Performance: Both if-else and ternary operators offer the same performance in most cases, but ternary operators are useful when writing compact code.

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?