Code
#include<iostream>
int main()
{
int a=4,b,c;
a==3 ? : b=a*a ; // Works fine
a==4 ? c=a*a : ; // ERROR Expected Primary expression before ;
}
1st conditional statement
I did not write 'Expression 2' but it will not produce error
2nd Conditional statement
I did not write 'Expression 3' it gives an error
so why it is differentiating in 'Expression 2' and 'Expression 3' ?
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.
The ternary operator, also known as the conditional operator, is used as shorthand for an if...else statement. A ternary operator is written with the syntax of a question mark ( ? ) followed by a colon ( : ), as demonstrated below. In the above statement, the condition is written first, followed by a ? .
Example: C Ternary Operator Here, age >= 18 - test condition that checks if input value is greater or equal to 18. printf("You can vote") - expression1 that is executed if condition is true. printf("You cannot vote") - expression2 that is executed if condition is false.
A ternary operator takes three arguments. The first one (1st) is the condition, the second one (2nd) is executed if the condition is true, and the third one (3rd) is executed if the condition is false. In layman's term, it's just an {if-else} statement in one (1) line!
Syntax of ternary operator is − (expression-1) ? expression-2 : expression-3 This operator returns one of two values depending on the result of an expression.
Then, the ternary operator is used to check if number is even or not. Since, 2 is even, the expression ( number % 2 == 0) returns true. We can also use ternary operator to return numbers, strings and characters. Instead of storing the return value in variable isEven, we can directly print the value returned by ternary operator as,
Note: A ternary expression is not a replacement for an if/else construct – it’s an equivalent to an if/else construct that returns a value. That is, an if/else clause is code, a ternary expression is an expression, meaning that it returns a value.
It's usually called the ternary operator because it's the only operator that takes three arguments. I'm talking about ? : operator (I always called it ternary operator: am I wrong?) +1 for accuracy. Yes, it's called the conditional operator.
This is supported in GNU C/C++ and is called the Elvis operator. It's not allowed in standard C++ (e.g. MSVC++ doesn't support it).
It's described in the official documentation
6.8 Conditionals with Omitted Operands The middle operand in a conditional expression may be omitted. Then if the first operand is nonzero, its value is the value of the conditional expression.
Therefore, the expression
x ? : y
has the value of x if that is nonzero; otherwise, the value of y.
This example is perfectly equivalent to
x ? x : y
In this simple case, the ability to omit the middle operand is not especially useful. When it becomes useful is when the first operand does, or may (if it is a macro argument), contain a side effect. Then repeating the operand in the middle would perform the side effect twice. Omitting the middle operand uses the value already computed without the undesirable effects of recomputing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With