Implementing ternary operator without any conditional statement Last Updated : 16 Jun, 2022 Comments Improve Suggest changes Like Article Like 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> Output10 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)) Output20 10 Asked In : Nvidia Comment More infoAdvertise with us Next Article Company Preparation R Rohit Thapliyal Improve Article Tags : DSA Nvidia cpp-operator cpp-puzzle Practice Tags : Nvidiacpp-operator Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like