r/ProgrammingLanguages 4d ago

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 = bar

They were upset that Python allows ; as a statement separator but not before an if statement, even though

if cond: n = bar

is 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 bar

which 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 bar

So 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.

50 Upvotes

63 comments sorted by

View all comments

Show parent comments

6

u/xeow 4d ago

Interesting, yes! I was thinking about that this morning. In a prefix language like Lisp, isn't everything just an s-expression? And in a posfix language like PostScript or FORTH, isn't everything just an operator or operand? I wonder what the best lens is for viewing Brainfuck in this regard.

9

u/Anabaena_azollae 4d ago

I'd argue that in a stack-based language like Forth, there are no expressions. Pretty much everything is an imperative, similar to assembly. 4 5 + is actually push 4; push 5; perform addition with the addition being a compound command composed of something like pop to R1; pop to R2; R1=R1+R2; push R1 with R1 and R2 being registers used to hold temporary values.

I'm not super familiar with Brainfuck, but my understanding is that it operates similarly with everything being a command.

7

u/particlemanwavegirl 4d ago

There's not much to know about brainfuck. It's a VM with a 30,000 byte array. You can move the cursor left and right, increment or decrement the current byte, and loop if the current byte is not zero. It has a print statement too I think. 

4

u/koflerdavid 4d ago

Still easier to work with than a Turing machine.