May 12, 2018

Conditional Operator in C


   Conditional operator is also known as ternary operator ( ? and : ) which requires three expressions as operands.


Syntax:

TestExpression ? expression1 : expression2



The conditional operator works as follows: 

  • TestExpression is evaluated first.
  • If the TestExpression evaluates to true(nonzero), then expression1 is evaluated and it becomes the value of overall conditional expression.
  • If TestExpression evaluates to false(zero), then expression2 is evaluated and it becomes the value of overall conditional expression.


Flow Diagram:

Conditional Operator Flow Diagram



Example 1:


int a = 7, b = 2;
a>b ? a : b


First the expression a>b is evaluated, since a>b is true so the value of variable a becomes the value of the overall conditional expression.


Conditional Operator Example 1


Since a > b ? a : b is an expression, we can assign its value to a variable.

large = a > b ? a : b;

The conditional expression above performs the same operation as the following if/else statement:

if(a>b)/* Expression a>b tested */
    large=a; /* Executes if a>b is true */
else
    large=b; /* Executes if a>b is false */


The following program demonstrates how to find greatest of two numbers using conditional operator:

#include <stdio.h>

int main(void)
{
    int a, b, large;

    printf("Enter a and b : ");
    scanf("%d%d", &a, &b);

    large = a > b ? a : b;

    printf("Largest of the two numbers = %d\n", large);

    return 0;
}


Output:

Enter a and b : 15 12
Largest of the two numbers = 15



Example 2:

int a=3, b=2, c=1, d=2;
a > b ? (c = 5) : (d = 4
)

Here first the expression a>b is evaluated. Since the expression a>b is true, 5 is assigned to c.


Conditional Operator Example 2


The conditional expression above performs the same operation as the following if/else statement:

if(a>b)/* Expression a>b tested */
    c = 5; /* Executes if a>b is true */
else
    d = 4; /* Executes if a>b is false */

The following program demonstrates conditional operator:

#include <stdio.h>

int main(void)
{
    int a=3, b=2, c=1, d=2;
   
    printf("Initial Value of c = %d\n", c);
    printf("Initial Value of d = %d\n\n", d);

    a>b ? (c = 5) : (d = 4);

    printf("After testing conditional expression: \n\n");

    printf("Value of c = %d\n", c);
    printf("Value of d = %d\n", d);


    return 0;
   
}


Output:

Initial Value of c = 1
Initial Value of d = 2

After testing conditional expression:

Value of c = 5
Value of d = 2




Nesting of Conditional Operators
       
The general form of a nested conditional operators is,

TestExpression1  ?  (TestExpression2 ? expression3 : expression4) 
                 :  (TestExpression3 ? expression5 : expression6)

Here is how conditional operator works:

  • TestExpression1 is evaluated first.
  • If the TestExpression1 evaluates to true(nonzero), then (TestExpression2 ? expression3 : expression4)  is evaluated.
  • If TestExpression1 evaluates to false(zero), then (TestExpression3 ? expression5 : expression6) is evaluated.


Nesting of Conditional Operators



Let's take an example:

int a=5, b=7, c=4;

Suppose we have following logical expression to find the greatest of three numbers:

large=((a>b)?(a>c ? a : c)
            :(b>c ? b : c))
;

Nesting of conditional operators example


The conditional expression above performs the same operation as the following if/else statement:

if (a > b)
{
    if (a > c)
        large = a;
    else
        large = c;
}
else
{
    if (b > c)
        large = b;
    else
        large = c;
}


The following program demonstrates how to find greatest of three numbers using conditional operator:

#include <stdio.h>

int main(void)
{
    int a, b, c, large;

    printf("Enter a, b and c : ");
    scanf("%d%d%d", &a, &b, &c);

    large=((a>b)?(a>c ? a : c)
                :(b>c ? b : c));

    printf("Largest of the three numbers = %d\n", large);

    return 0;
}


Output:

Enter a, b and c : 15 12 17
Largest of the three numbers = 17



Conditional operators make the program code more compact, more readable, and safer to use as it is easier both to check and guarantee the arguments that are used for evaluation.



next    Comma Operator in C
top    Index
prev    Logical Operators in C





1 comment:

  1. Well define and your grapichal example is very easiest way to understand all that thing. THANKS

    ReplyDelete