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(){ 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;} |
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;} |
m is greater than n that is 5 > 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Implementing ternary operator without any conditional statement
- Set a variable without using Arithmetic, Relational or Conditional Operator
- C/C++ Ternary Operator - Some Interesting Observations
- C++ | Nested Ternary Operator
- Program to Find the Largest Number using Ternary Operator
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- 3-way comparison operator (Space Ship Operator) in C++ 20
- Print "Even" or "Odd" without using conditional statement
- Conditionally assign a value without using conditional and arithmetic operators
- Output of C programs | Set 55 (Ternary Operators)
- Operands for sizeof operator
- Copy constructor vs assignment operator in C++
- Self assignment check in assignment operator
- Rules for operator overloading
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- Is assignment operator inherited?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sharmaparth5399



