0% found this document useful (0 votes)
7 views2 pages

Conditional or Ternary Operator

The conditional or ternary operator in C, denoted as '?:', allows for a more concise way to write if-else statements by evaluating a condition and executing one of two expressions based on the result. It operates on three operands and can be used in various syntactical forms. An example demonstrates its use to determine and print the greater of two numbers.

Uploaded by

aryankondekar15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Conditional or Ternary Operator

The conditional or ternary operator in C, denoted as '?:', allows for a more concise way to write if-else statements by evaluating a condition and executing one of two expressions based on the result. It operates on three operands and can be used in various syntactical forms. An example demonstrates its use to determine and print the greater of two numbers.

Uploaded by

aryankondekar15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Conditional or Ternary Operator (?

:) in C

The conditional operator in C is kind of similar to the if-else statement as it
follows the same algorithm as of if-else statement but the conditional operator
takes less space and helps to write the if-else statements in the shortest way
possible. It is also known as the ternary operator in C as it operates on three
operands.
Syntax of Conditional/Ternary Operator in C
The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;
Or the syntax can also be in this form
variable = (condition) ? Expression2 : Expression3;
Or syntax can also be in this form
(condition) ? (variable = Expression2) : (variable = Expression3);

Conditional/Ternary Operator in C

It can be visualized into an if-else statement as:


if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are
also called ternary operators.

Working of Conditional/Ternary Operator in C


The working of the conditional operator in C is as follows:
 Step 1: Expression1 is the condition to be evaluated.
 Step 2A: If the condition(Expression1) is True then Expression2 will be
executed.
 Step 2B: If the condition(Expression1) is false then Expression3 will be
executed.
 Step 3: Results will be returned.
Flowchart of Conditional/Ternary Operator in C
To understand the working better, we can analyze the flowchart of the conditional
operator given below.

Flowchart of conditional/ternary operator in C

Examples of C Ternary Operator


Example 1: C Program to Store the greatest of the two Numbers using the
ternary operator
C
// C program to find largest among two
// numbers using ternary operator

#include <stdio.h>

int main()
{
int m = 5, n = 4;

(m > n) ? printf("m is greater than n that is %d > %d",


m, n)
: printf("n is greater than m that is %d > %d",
n, m);

return 0;
}

Output
m is greater than n that is 5 > 4

You might also like