Today I read this line of Java code:
if (results.add(userName));
For me, it is the first if statement I’ve ever seen which does not have a then or else clause. I was surprised that it is not a syntax error.
So is there any use of this kind of if statement at all? That is, an if without then or else clause.
No use. It’s very likely a typo. It’s not a special if statement, however. The
;is a statement, the empty statement, which from the language’s point of view, is equivalent to an empty block{}.