4 ms·
If this compiles, <something> is an expression: let x = <something>; If this compiles, <something> is a statement: <something>; Both, either, or nei
by bluepnume 4y ago
If this compiles, <something> is an expression:
let x = <something>;
If this compiles, <something> is a statement:
<something>;
Both, either, or neither can be true.
- hibbelig 4y agoI can't think of an example where let x = <something>; compiles but <something>; does not compile. I think all expressions are statements.
- lmm 4y agoSome languages forbid "obviously pure" expressions like "0" as statements (since using them as a statement is probably a mistake).
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- bluepnume 4y agoRight. They're not mutually exclusive -- hence the "both, either, or neither"
- hibbelig 4y agoMy 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.
- FeepingCreature 4y agoSo 'if', 'for', 'while' are not statements?
- teddyh 4y agoNo. Those are syntactic elements.
- lmm 4y agoNot 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.
- FeepingCreature 4y agoThe body is also not terminated by a semicolon. `while (true) { puts("Hello World\n"); };`
- lmm 4y agoWhat you've written there does compile though, which was the original condition :).
- FeepingCreature 4y agoI get the joke, but it's two statements, not one. (while and ;)
- shikoba 4y agoIn JavaScript no. But in some languages it can be.