Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator, if I avoid writing ' expression 2 ' it works but if I not write 'expression 3 ' it gives an error [duplicate]

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' ?

like image 456
Abhishek Mane Avatar asked Apr 08 '21 06:04

Abhishek Mane


People also ask

Is ternary operator statement or expression?

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.

How do you write ternary operator if else?

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 ? .

What is a ternary operator in C give an example expression for its use?

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.

How does ternary operator work as like as if else statement?

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!

What is the syntax of ternary operator in JavaScript?

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.

How to use the ternary operator to return a number?

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,

What is the difference between IF/ELSE and ternary expression?

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.

What is the operator with three arguments called?

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.


1 Answers

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.

like image 50
Thomas Sablik Avatar answered Oct 08 '22 18:10

Thomas Sablik