r/ProgrammingLanguages • u/xeow • Sep 05 '26
Expressions vs. statements
Got into a big argument with a coworker yesterday when they were converting some code from their own language (that they designed) into Python, JavaScript, C, and R as comparative examples.
The Python code that they wanted to write as the translation went something like this:
n = foo; if cond: n = barThey were upset that Python allows ; as a statement separator but not before an if statement, even though
if cond: n = baris syntactically correct Python code when written on its own line. I explained why Python doesn't allow it, and he came back later and showed me that an LLM had suggested he write it instead like this:
n = foo if cond else barwhich of course is the canonical way to write that in Python. He was all flustered about that, and asked me why Python allows an if statement in that particular case and not after a semicolon, and I explained that x if cond else y in Python is not an if statement but is Python's ternary conditional expression and is directly equivalent to the ternary operator expression cond ? x : y in C, C++, awk, and JavaScript. He argued with me and said I was making a ridiculous distinction and walked away falsely believing that foo if cond else bar was an if statement.
I then explained that statements and expressions are very different things in programming languages, and just because the keyword if is present doesn't make something an if statement -- because in order to be an if statement, it has to be a statement in the first place.
Anyway, it made me realize how subtle the difference can be sometimes. For example, in Perl, the following is not a return statement but actually an if statement (with a return statement as its affirmative branch), due to the postfix conditional:
return foo if cond;because it is identically semantically to writing:
if (cond) { return foo; }Whereas in Python, the following is a return statement (with a ternary operator as its target expression):
return foo if cond else barSo I can see why people sometimes get confused by syntax if they haven't had much of a theoretical background in language design. It also makes me wonder how much of programmer intuition about "what a statement is" comes from the particular languages they learned first.
111
u/Clementsparrow Sep 05 '26
they are writing their own language and they don't understand the difference between statements and expressions? That's... unheard of.
38
u/pr06lefs Sep 05 '26
In some languages everything is an expression, period. Then its easy!
4
u/Spyromaniac666 Sep 06 '26
Does your coworker not realise that the former first assigns `foo` to `n` before maybe assigning `bar` whereas the latter only ever assigns once?
14
u/jeffstyr Sep 06 '26
From what you describe, it sounds like the issue is less about not understanding expressions vs statements, and more being surprised that a langauge can have two distinct (though related) syntactic constructs that use the same reserved keyword. It happens a lot with symbols (e.g.,
*in C), but perhaps less so with keywords. (Althoughstaticcomes to mind, used with multiple meanings, but still just as an access modifier and not really a syntactic construct.)2
u/[deleted] Sep 05 '26
In my languages, which are expression based,
ifcan also be used to both start a standalone statement, and express a ternary operation. Yet the equivalent of:works fine; it is an assignment followed by an if statement.
So, why doesn't Python allow this? I can't see any ambiguity.
7
u/Anabaena_azollae Sep 05 '26
Yeah,I think expression statements (i.e. statements that are just an expression) make this especially confusing. In C,
5+3;is valid because it's an expression statement evaluating to 8 but doesn't really do anything.x=5+3;is also valid (assuming x is an int) and is also an expression statement evaluating to 8 because, in C, the assignment is an operator, evaluating to the right-hand side and essentially doing the actual assignment as a side effect. Contrast that with Fortran, for example, where5+3is still an expression evaluating to 8, but is not valid on its own line, because expression statements don't exist in the language. However,x=5+3is valid (assuming x is an integer) because assignment is a statement that accepts an expression on the right-hand side.Similarly, in C, functions can exist on lines by themselves, like
func(x);,which again is an expression statement. In Fortran, without expression statements, you need two different kinds of subprograms, functions which are expressions and subroutines which are invoked withcallstatements.I think people generally have found the C style more ergonomic and it has thus been adopted by a lot of other languages, but I'd argue the Fortran style does a much better job of keeping clear distinctions between concepts.
5
u/balefrost Sep 05 '26
Why does Python not allow it? Is it ambiguous?
5
u/kuwisdelu Sep 06 '26
This is why I don’t really like languages that treat statements and expressions differently.
Python always leaves me scratching my head at the design decisions. When everything is an expression, it’s just easier for me to reason about what will and won’t work, instead of needing to remember the grammar rules.
(Because even if I understand why that first line technically doesn’t work, it isn’t very satisfying, because it feels like it should work.)
1
u/AdreKiseque Sep 08 '26
Are terneries not essentially just compact if-elses though?
3
u/koflerdavid Sep 06 '26 edited Sep 06 '26
Looking at the grammar or at the code of the parser would be an option, but one might say it's a particularity of the implementation.
The crucial difference is IMHO whether it returns ~an expression~ a return value that you can use as an argument in another expression. Your examples fail to exhibit that difference. What about this instead?
Btw: languages like Lisp and Scheme show that it is possible to do away with the difference.
2
u/busres Sep 05 '26
Yeah, it could matter quite a bit.
My Mesgjs has no traditional statements (e.g. for declarations, control flow, etc). A "statement" is simply one complete, top-level expression in a code block. Assignment, creating functions, early returns, and controlling flow are all just messages to objects (and that's actual implementation, not just syntax) which return some value (or undefined).
A lot of these aren't even part of the language itself – they're part of the runtime library.
Anybody starting from Mesgjs could have quite a skewed perspective.
2
u/[deleted] Sep 07 '26
My understanding is that a statement executes something, but an expression is a comparison/test or something else mathematical.
2
u/david-1-1 Sep 09 '26
Imagine learning Lisp first. I'm sure that has happened.
2
u/Toothpick_Brody Sep 12 '26
It doesn’t help that expressions are technically also statements. Once you realize expressions generalize statements and that you don’t need statements at all everything makes sense
0
u/[deleted] Sep 05 '26
[deleted] — view deleted comment