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.
+ 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.
+ 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/.
+ 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
+ 8
if(condition) then value1 else value2
remove if, replace then with ? and replace else with :
(condition) ? value : value2
+ 6
Vishnu Sai Yes, it is. Ternary operator can replace the usage of if else statements thus making your codes shorter
+ 5
hi,it is a conditional operator
by following syntax
(condition)? statement1: statement 2;
it like if else statement
+ 4
always remember about "return"
+ 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
+ 2
just like if condition on Excel
+ 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.
+ 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