Open In App

Implementing ternary operator without any conditional statement

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.
 

1. Using Binary Operator

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

2. Using Array

int arr[] = { b, a };

We can return the value present at index 0 or 1 depending upon the value of a.

  • For a= 1, the expression arr[a] reduces to arr[1] = b.
  • For a= 0, the expression arr[a] reduces to arr[0] = c.
     

C




#include <stdio.h>
  
int ternary(int a, int b, int c)
{
    int arr[] = { c, b };
    return arr[a];
}
  
int main(void)
{
    int a = 10, b = 20;
    printf ("%d\n", ternary(0, a, b));
    printf ("%d\n", ternary(1, a, b));
    return 0;
}


C++




#include <iostream>
using namespace std;
 
int ternary(int a, int b, int c)
{
    int arr[] = { c, b };
    return arr[a];
}
  
int main(void)
{
    int a = 10, b = 20;
    cout<<ternary(0, a, b)<<endl;
    cout<<ternary(1, a, b)<<endl;
    return 0;
}


Python3




def ternaryOperator( a, b, c):
  arr = [ c, b ]
  return arr[a]
   
# Driver code
if __name__ == "__main__":
  a = 1
  b = 10
  c = 20
  print(ternaryOperator(a, b, c))
  a = 0
  print(ternaryOperator(a, b, c))


Output

20
10

Asked In : Nvidia 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads