How to implement ternary operator in C++ without using conditional statements.
In the following condition : a ? b : c
If a is true, b will be executed.
Otherwise, c will be executed.
We can assume a, b and c as values.
We can code the equation as :
Result = (!!a)*b + (!a)*c
In above equation, is a is true, result will be b.
Otherwise, result will be c.
Asked In : Nvidia
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Conditional or Ternary Operator (?:) in C/C++
- Print "Even" or "Odd" without using conditional statement
- C++ | Nested Ternary Operator
- C/C++ Ternary Operator - Some Interesting Observations
- Maximum of four numbers without using conditional or bitwise operator
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Program to Find the Largest Number using Ternary Operator
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Conditionally assign a value without using conditional and arithmetic operators
- Implementing Forward Iterator in BST
- C/C++ if else statement with Examples
- C/C++ if statement with Examples
- Continue Statement in C/C++
- goto statement in C/C++
- Break Statement in C/C++