Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10
The conditional (ternary) operator which inherited from C has the following form condition ? expression-true : expression-false - condition part must be of scalar type* - expression-true will be evaluated if the condition's result be unequal to zero (e.g. 36, 5.2, true, etc.) - expression-false will be evaluated if the condition's result be equal to zero (e.g. 0, false) ~Important notes~ - In case of arithmetic types which are widely used, expression-true and expression false must be of the same type. (there are some exceptions) - the entire conditional operator must be wrapped in a pair of parenthesis when is intended to send to an ostream object (e.g. cout) ~examples~ x > y ? 2 : 30; // valid x > y ? 2 : 5.1; // valid x > y ? 2 : "123"; // error cout << x > y ? 2 : 7; // error cout << (x > y ? 2 : 7); // valid ______ * Arithmetic types (boolean, character, integer, floating-point) and pointer types.
23rd Jun 2018, 10:02 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 14
As the name suggests ternary operator is an operator that takes three arguments. First argument is the comparison, second argument is result if comparison is true and third argument is result if comparison is false. For eg. You can compare it to if-else statements. In short, ternary operator has three parts. Condition, result if condition is true and, result if condition is false.
22nd Jun 2018, 3:15 PM
👑 Ak-king 👑
👑 Ak-king 👑 - avatar
+ 8
The ternary operator tests a certain condition. If the condition is true it returns the value after "?", if it is false it returns the value after ":". You can read more here www.cplusplus.com/forum/articles/14631/.
22nd Jun 2018, 3:19 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 8
it's possible that you add a condition if first condition is false example : a = (cond)? value:(cond)?value :value; and you can test as many condition as you want
22nd Jun 2018, 3:32 PM
aziz
aziz - avatar
+ 8
if(condition) then value1 else value2 remove if, replace then with ? and replace else with : (condition) ? value : value2
22nd Jun 2018, 6:28 PM
Vikash Pal
Vikash Pal - avatar
+ 6
Vishnu Sai Yes, it is. Ternary operator can replace the usage of if else statements thus making your codes shorter
23rd Jun 2018, 10:11 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 5
hi,it is a conditional operator by following syntax (condition)? statement1: statement 2; it like if else statement
23rd Jun 2018, 3:25 AM
Vishnu Sai
Vishnu Sai - avatar
+ 4
always remember about "return"
21st Aug 2018, 8:49 AM
Konstantinos Fuad
Konstantinos Fuad - avatar
+ 3
...Continue... Precedence of conditional operator is from right to left, which makes it possible to perform chaining evaluation. str = a == 1 ? "foo" : // if b == 2 ? "boo" : // else if c == 3 ? "zoo" : // else if "doo"; // else
23rd Jun 2018, 10:20 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 2
just like if condition on Excel
23rd Jun 2018, 6:16 PM
Akhmad Fauzi Hasibuan
Akhmad Fauzi Hasibuan - avatar
+ 2
The ternary operator is the shorthand of if else statement. condition ? expression1 : expression2 I commonly used in web if you want to hide a button. Let's say the value of display attribute is none. <button style={{style: (display: (value !== "none"? "" : "none"))}}Test Button</button> In this example, the button will be hidden because we set the display attribute to none. Otherwise, it will set to an empty string then it will show up the button.
24th Jun 2018, 2:45 PM
Winston Jade Molit
Winston Jade Molit - avatar
+ 2
It is also a condition like if else syntax : (condition) ? statement 1 : statement 2 explanation : if the condition is true it will return statement 1 , otherwise statement 2. example: x=5,y=6 x(5) > y(6) ? x(5) : y(6)//returns 6
7th Jul 2018, 1:32 PM
லாவண்யா ☺☺☺
லாவண்யா ☺☺☺ - avatar