The language specification (Aug 19, 2015) states that an assert
statement contains a parenthesized conditionalExpression
. This feature changes assert
to use a plain expression
instead.
The motivation for having a conditionalExpression
rather than a general expression
in this syntactic location is not discussed in the specification, but it is likely to be a kind of safety device: A conditionalExpression
covers only a subset of all expressions, and in particular it does not include assignments. An example which is often mentioned is that assert(x = 1)
will be prevented syntactically, and the programmer may then note the error and change it to the more likely assert(x == 1)
.
However, the language specification takes different approaches in other, similar situations: An if
statement, while
statement, and do .. while
statement all accept a general expression
as the condition, whereas an argument to logical operators such as &&
and ||
must be