0% found this document useful (0 votes)
298 views

Conditional or Ternary Operator ( - ) in C - C++

The conditional or ternary operator in C/C++ is a shorthand for an if-else statement that takes up less space. It takes three operands - an expression to evaluate, an expression if true, and an expression if false. If the condition is true, it returns the second expression, otherwise it returns the third expression. For example, to find the largest of two numbers n1 and n2, it can be written as max = (n1 > n2) ? n1 : n2; which sets max to n1 if n1 is greater than n2, otherwise it sets max to n2.

Uploaded by

Krishanu Modak
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)
298 views

Conditional or Ternary Operator ( - ) in C - C++

The conditional or ternary operator in C/C++ is a shorthand for an if-else statement that takes up less space. It takes three operands - an expression to evaluate, an expression if true, and an expression if false. If the condition is true, it returns the second expression, otherwise it returns the third expression. For example, to find the largest of two numbers n1 and n2, it can be written as max = (n1 > n2) ? n1 : n2; which sets max to n1 if n1 is greater than n2, otherwise it sets max to n2.

Uploaded by

Krishanu Modak
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/ 3

Conditional or Ternary Operator (?:) in C/C++


The conditional operator is kind of similar to the if-else statement as it does follow 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.

Syntax:
The conditional operator is of the form

variable = Expression1 ? Expression2 : Expression3

It can be visualized into 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:
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then Expression2 will be executed and the result will
be returned. Otherwise, if the condition(Expression1) is false then Expression3 will be executed and the result will be returned.
Example: Program to Store the greatest of the two Number.

C
// C program to find largest among two
// numbers using ternary operator
  
#include <stdio.h>
  
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, max;
  
    // Largest among n1 and n2
    max = (n1 > n2) ? n1 : n2;
  
    // Print the largest number
    printf("Largest number between"
           " %d and %d is %d. ",
           n1, n2, max);
  
    return 0;
}

C++

// C++ program to find largest among two


// numbers using ternary operator
  
#include <iostream>
using namespace std;
  
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, max;
  
    // Largest among n1 and n2
    max = (n1 > n2) ? n1 : n2;
  
    // Print the largest number
    cout << "Largest number between "
         << n1 << " and "
         << n2 << " is "
         << max;
  
    return 0;
}

Output:
Largest number between 5 and 10 is 10.

You might also like