What is the name of this code construct: condition? true_expression: false_expression

What is the correct term / name for the following construct:

string myString = (boolValue==true ? "true": "false"); 
+6
source share
9 answers

This is a ternary conditional expression.

+15
source

C, C ++, C #, and Java standards use the following terms:

  • The operator is a conditional operator.
  • Such an expression is a conditional expression.

So these are the official names. Programmers often more weakly refer to it as a ternary operator, since it is the only widely used operator with three operands. Strictly speaking, this is just a ternary operator.

+10
source

The ?: Operator is a "conditional operator".

boolValue==true ? "true": "false" boolValue==true ? "true": "false" is therefore a conditional expression.

myString = (boolValue==true ? "true": "false") is a conditional expression that also has a job.

string myString = (boolValue==true ? "true": "false"); is an operator that uses a conditional expression in a declaration and assignment.

?: often called a "triple operator". Strictly this is only a ternary operator, but since it is unique in these languages, the appeal to the ternary operator is true, although it uses a label based on a fact separate for its own internal definition. If we add an operator to a language of type x Β§ y ΒΆ z , which took all three of x , y and z as operands, then it would also be a ternary operator, and the conditional operator would still be a ternary operator, it would no longer be ternary the operator.

+6
source

This is called the ternary conditional operator . I do not know if the expression using it has a specific name.

Hope this helps!

+5
source

I'm not sure if this is a common language in the C # community, but in C and C ++ many people usually call this the triple operator. Why?

  • There are unary operators that expect a single operand, for example, in -x .
  • There are binary operators that expect two operands, for example, in x+x .
  • There is only one ternary operator that expects three operands, for example, in x?y:z .

The "correct" name is the "conditional operator", because the result depends on the condition (the leftmost operand).

+2
source

I think this is called a "triple operator".

+1
source

This is a conditional expression, but when do you use "?" called the "Conditional statement".

+1
source

This is a conditional expression .

For readability, put a difficult condition in brackets:

 string myString = (somecond)?"true":"false"; 

In your case, just check boolValue for

 string MyString = boolValue?"true":"false". 
+1
source

It was called β€œternary,” and as far as I remember, when I learned Java (11/12 years ago), you couldn't do it, or at least the teachers didn't teach it at all. Maybe it's because sometimes the code is less readable ...

0
source

Source: https://habr.com/ru/post/906279/


All Articles