What are these called [duplicate]

2020-04-03 04:20发布

Possible Duplicate:
What does ‘?’ do in C++?

What are these kind of statements in c++ called:

testNumber > 1 ? true : false;

9条回答
2楼-- · 2020-04-03 04:31

(As an annoying nitpicker: For some reason, despite the fact that the question is about the statement, most people give answers about the operator.)

Firstly, what you have in your example is indeed a statement. It is called an expression statement. There are different kinds of statements in C++ (declaration statement, jump statement, iteration statement etc.), and this one happens to be an expression statement. So, if your question is indeed about what kind of statement that is, the pedantically correct answer would be: it is an expression statement. End of story.

Now, secondly, if you want to go deeper, you might want to pick apart the expression that is used in this expression statement. The expression in this case has a conditional ?: operator at the top level. The first operand to that operator is a sub-expression using a relational > operator... and so on.

查看更多
够拽才男人
3楼-- · 2020-04-03 04:32

ternary statements

查看更多
神经病院院长
4楼-- · 2020-04-03 04:34

It's an expression with a ternary (or conditional) operator in it.

查看更多
疯言疯语
5楼-- · 2020-04-03 04:44

Conditional Expression

      Conditional Expression
   <------------------------>
    (x < y)    ?      x : y
    |_____|    |________|
     Test       Conditional
    Expression   Operator

The '?' and ':' make up the conditional operator.

查看更多
Juvenile、少年°
6楼-- · 2020-04-03 04:44

On Language Syntax

What are these kind of statements in c++ called:

You've asked a question with multiple answers. Technically it would depend on the actual language used but what you've shown is very typical to a large number of languages that have a common syntax.

Statements

Statements are just a way of indicating a block of tokens that can be thought of as a single unit. Statements typically end with a semi-colon ";". Some statements are simple like

print 0;

which has one keyword and one literal, while others are more complex like

a = foo(c*10) + 5;  

which has an assignment encompassed by a return sub-expression and an arithmetic sub-expression, a function call with an argument resolution involving a multiplication sub-expression with a variable lookup as one of the operands.

Your statement is the results of an expression.

testNumber  >  1  ?  true  :  false       ;
====================================      ======================  
expression that needs evaluated           end-of-statement marker  

Conditional Operator

The conditional operator is sometimes referred to as the 'Ternary Operator'. It has an operator and three (hence ternary) operands.

testNumber > 1     ?    true     :    false   
==============    ---   =====   ---   ======
      |            |      |      |      |
      |            |    Exp2     |     Exp3
      |            |             |      
      |            └   Operator  ┘ 
      |
Exp1 (which has to be a boolean expression)

The results of either Exp1 or Exp2 is returned as the value of the conditional operator expression, Exp1 if "Condition" is true, Exp2 otherwise.

Explanation

The Conditional (ternary) Operator may not be as familiar to coders for various reasons.

One of its typical uses is to simplify code when we want to return different things based on some condition.

For example, given

a = 0;
b = 5;

we can easily return a division result in one line

return (a == 0) ? NaN : b/a;  //Not-A-Number

We could just build the return with the expression

return (b/a);

but we want to guard against division by zero. Using an if/else block would be code bloat.

if ( a == 0)
{
  return NaN;
}
else
{
  return b/a;
}

Related questions

To ternary or not to ternary?
Which coding style you use for ternary operator?
Is this a reasonable use of the ternary operator?

查看更多
登录 后发表回答