Re:Statements vs. Expressions
by bluepnume on Thursday July 14, 2022 @05:43AM
(#32093909)
If this compiles, <something> is an expression:
If this compiles, <something> is a statement:
Both, either, or neither can be true.
[ Parent
| on HN ]
I can't think of an example where
let x = <something>;
compiles but <something>;
does not compile. I think all expressions are statements.Some languages forbid "obviously pure" expressions like "0" as statements (since using them as a statement is probably a mistake).
I think I was saying that Javascript is not one of them.
Interesting! Any examples of such languages?
Java is one popular example. You get "error: not a statement".
Right. They're not mutually exclusive -- hence the "both, either, or neither"
My point was that there are four combinations, and "both, either, or neither" suggests that all four can happen. But actually, only three out of four can happen.
It can be both a statement and an expression. Yes.
It can be neither a statement nor an expression. Yes.
It can be a statement but not an expression. Yes.
But the case that it is an expression but not a statement: this case never happens.
Re:Statements vs. Expressions
by FeepingCreature on Thursday July 14, 2022 @05:50AM
(#32093959)
So 'if', 'for', 'while' are not statements?
Not on their own. A full if, for or while (with the conditions and body) is usally a statement, and might be an expression in some languages.
Re:Statements vs. Expressions
by FeepingCreature on Friday July 15, 2022 @06:17AM
(#32106580)
The body is also not terminated by a semicolon.
`while (true) { puts("Hello World\n"); };`
What you've written there does compile though, which was the original condition :).
Re:Statements vs. Expressions
by FeepingCreature on Saturday July 16, 2022 @05:06AM
(#32116349)
I get the joke, but it's two statements, not one. (while and ;)
In JavaScript no. But in some languages it can be.