Skip to content
Related Articles

Related Articles

Improve Article

Implementing ternary operator without any conditional statement

  • Difficulty Level : Medium
  • Last Updated : 28 Apr, 2021

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, if a is true, the result will be b. 
Otherwise, the result will be c.
 

C++




// CPP code to implement ternary operator
#include <bits/stdc++.h>
 
// Function to implement ternary operator without
// conditional statements
int ternaryOperator(int a, int b, int c)
{
    // If a is true, we return (1 * b) + (!1 * c) i.e. b
    // If a is false, we return (!1 * b) + (1 * c) i.e. c
    return ((!!a) * b + (!a) * c);
}
 
// Driver code
int main()
{
    int a = 1, b = 10, c = 20;
     
    // Function call to output b or c depending on a
    std::cout << ternaryOperator(a, b, c) << '\n';
 
    a = 0;
     
    // Function call to output b or c depending on a
    std::cout << ternaryOperator(a, b, c);
     
    return 0;
}

Python 3




# Python 3 code to implement ternary operator
 
# Function to implement ternary operator
# without conditional statements
def ternaryOperator( a, b, c):
     
    # If a is true, we return
    # (1 * b) + (!1 * c) i.e. b
    # If a is false, we return
    # (!1 * b) + (1 * c) i.e. c
    return ((not not a) * b + (not a) * c)
 
# Driver code
if __name__ == "__main__":
 
    a = 1
    b = 10
    c = 20
     
    # Function call to output b or c
    # depending on a
    print(ternaryOperator(a, b, c))
 
    a = 0
     
    # Function call to output b or c
    # depending on a
    print(ternaryOperator(a, b, c))
     
# This code is contributed by ita_c

PHP




<?php
// PHP code to implement
// ternary operator
 
// Function to implement
// ternary operator without
// conditional statements
function ternaryOperator($a, $b, $c)
{
     
    // If a is true, we return
    // (1 * b) + (!1 * c) i.e. b
    // If a is false, we return
    // (!1 * b) + (1 * c) i.e. c
    return ((!!$a) * $b + (!$a) * $c);
}
 
    // Driver code
    $a = 1;
    $b = 10;
    $c = 20;
     
    // Function call to output
    // b or c depending on a
    echo ternaryOperator($a, $b, $c) ,"\n";
 
    $a = 0;
     
    // Function call to output b
    // or c depending on a
    echo ternaryOperator($a, $b, $c);
     
// This code is contributed by jit_t.
?>

Javascript




<script>
 
// Javascript code to implement
// ternary operator
     
    // Function to implement
    // ternary operator without
    // conditional statements
    function ternaryOperator(a,b,c)
    {
        // If a is true,
        // we return (1 * b) + (!1 * c) i.e. b
        // If a is false,
        // we return (!1 * b) + (1 * c) i.e. c
         
        return ((!!a) * b + (!a) * c);
    }
     
    // Driver code
    let a = 1, b = 10, c = 20;
    // Function call to output b or c depending on a
    document.write( ternaryOperator(a, b, c)+"<br>");
     
    a = 0;
    // Function call to output b or c depending on a
    document.write( ternaryOperator(a, b, c)+"<br>");
     
    // This code is contributed by avanitrachhadiya2155
     
</script>

Output:  

10
20

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 contribute@geeksforgeeks.org. 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.
 

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :