If there's no problem treating a statement as an expression, why was there a distinction in the first place in some programming languages?

Posted by cdmckay on Programmers See other posts from Programmers or by cdmckay
Published on 2014-08-22T06:35:06Z Indexed on 2014/08/22 10:27 UTC
Read the original article Hit count: 193

Filed under:
|
|

Why do we have the distinction between statements and expressions in most programming languages?

For example, in Java, assuming f and g return ints, this still won't compile because it's a statement and statements don't return values.

// won't compile
int i = if (pred) { 
    f(x); 
} else {
    g(x);
}

but in Scala, it's very happy with treating if as an expression.

// compiles fine
val i: Int = if (pred) f(x) else g(x)

So if there's no problem treating an if statement as an expression, why was there a distinction in the first place?

© Programmers or respective owner

Related posts about java

Related posts about programming-languages