BLOG DETAILS

Read the complete blog details covering in-depth explanations, practical insights, and examples. Designed for computer science learners and technology enthusiasts who want clarity, guidance, and knowledge in simple, structured form.

Nesting of conditional operator (Ternary Operator) in C Language | Detailed & Practical

Definition:

The conditional operator is also known as "Ternary Operator". The conditional operator contains a condition followed by two statements or values. If the condition is true, the first statement is executed, otherwise the second statement is executed.

Syntax:

            CONDITION ? EXPRESSION-1 : EXPRESSION-2;

Example:

            a==0 ? printf("A is Zero") : printf("A is Not Zero");

Note:

The additional use of conditional operator is "SELECTIVE ASSIGNMENT".

Programming Example:

#include<stdio.h>
#include<conio.h>
void main()
{
        int a,b,max;
        clrscr();
        printf("Enter Two Number");
        scanf("%d%d",&a,&b);
        max=a>b?a:b;//Selective Assignment.
        printf("Maximum among %d and %d is %d",a,b,max);
}

Nesting of Conditional Operator (? :) : 

We know that the general form of the conditional operator is condition ? true part : false part. This form can also be interpreted as: If condition then statement else statement.

The nesting of conditional operator generally in two forms.

  • Nesting inside false part/else part.
    • Condition ? True Part : Condition ? True Part : False Part;
  • Nesting inside true part/if part.
    • Condition ? Condition ? True Part : False Part : False Part;
Suresh Kulahry

About the Author

Suresh Kulahry

Suresh Kulahry is the Founder of Suraku Academy and an educator dedicated to helping students prepare for Computer Science examinations through simple explanations, high-quality study material, MCQs, Previous Year Questions, and practical tutorials. His mission is to make computer science learning easy, accessible, and free for every learner.

🎉 Thank You for Reading!

We hope this article helped you understand the topic better. Continue your Computer Science preparation with more free resources available on Suraku Academy.