Possible Duplicate:
What does ‘?’ do in C++?
What are these kind of statements in c++ called:
testNumber > 1 ? true : false;
Possible Duplicate:
What does ‘?’ do in C++?
What are these kind of statements in c++ called:
testNumber > 1 ? true : false;
(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.ternary statements
It's an expression with a ternary (or conditional) operator in it.
Conditional Expression
The '?' and ':' make up the conditional operator.
On Language Syntax
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
which has one keyword and one literal, while others are more complex like
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.
Conditional Operator
The conditional operator is sometimes referred to as the 'Ternary Operator'. It has an operator and three (hence ternary) operands.
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
we can easily return a division result in one line
We could just build the return with the expression
but we want to guard against division by zero. Using an if/else block would be code bloat.
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?
Ternary operator. http://en.wikipedia.org/wiki/Ternary_operation