ternary statements
That's the conditional operator.
The expression a ? b : c
evaluates to b
if a
is true and c
if a
is false
.
In most languages, this is the only example of a ternary operator, an operator that takes three arguments.
It's an expression with a ternary (or conditional) operator in it.
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
http://stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary
http://stackoverflow.com/questions/243217/which-coding-style-you-use-for-ternary-operator
http://stackoverflow.com/questions/522919/is-this-a-reasonable-use-of-the-ternary-operator
Conditional Expression
Conditional Expression
<------------------------>
(x < y) ? x : y
|_____| |________|
Test Conditional
Expression Operator
The '?' and ':' make up the conditional operator.
(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 operator.
Your expression is for finding whether the value is true or false. For example,
boolean c = testNumber > 1 ? true : false;
Here based on the value of testNumber, c will be true or false.