News for hackers. Stuff that matters.
Re:Statements vs. Expressions by usrbinbash on Thursday July 14, 2022 @06:47AM (#32094273)
I think the simplest explanation is; "An expression can be used where a value is expected".

What is sometimes confusing, is that some statements also double as expressions, that is, also return values; and that it also depends on the language. eg. the assignment statement returns a value in C and Javascript, but not in Go or Python;

    printf("%d\n", a = 42);
    // out: 42

    console.log(a = 42)
    // out: 42

    fmt.Printf("%d\n", a = 42)
    // out: syntax error: unexpected =, expecting comma or )

    print("%d" % a = 42)
    # out: SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Also depending on the language, expressions themselves may be valid statements. For example, this compiles in C;

    int main() {
        12 + 42;
    }
;this is a valid python program:

    12 + 42
;but if I try this in Go:

    package main
    func main() {
        42
    }
    // ./main.go:3:5: 12 + 42 (untyped int constant 54) is not used
Re:Statements vs. Expressions by bluetomcat on Thursday July 14, 2022 @07:03AM (#32094360)
Generally, an expression returns a value and can be used as the operand of an operator, whereas a "statement" is the terminal "top-level" node that doesn't return a value and cannot therefore be nested.