1

Any computer language have 'expression' and 'statement'. in concept, what's the diff?

lovespring
  • 19,051
  • 42
  • 103
  • 153
  • 3
    The key thing is that statements are not allowed in expression contexts. And note that not all languages have statements. – Dan D. May 02 '12 at 06:41
  • 1
    I don't understand why so much people around here answer questions in comments instead of as...answers. – Klaim May 02 '12 at 06:43
  • 2
    @Klaim maybe because it's harder to downvote a comment :) – Ja͢ck May 02 '12 at 06:49
  • Pay close attention to what @DanD. wrote: not every computer language has expressions and statements. For instance, pure functional languages do not have statements and [Tcl](http://www.tcl.tk) does not have neither statements nor expressions (only commands) while being (mostly) imperative. – kostix May 02 '12 at 07:58
  • 1
    @Klaim, that's because sometimes an answer does not answer exactly what was asked: pointing out a problem with the statement made in a question (pun intended) is one such example ;-) – kostix May 02 '12 at 08:02
  • @Klaim Well, If you consider what I say to be an answer, I may not, and so I leave it to you to do what you will. – Dan D. May 02 '12 at 08:06

4 Answers4

5

Taken from the wikipedia :

In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all. Among imperative programming languages, Algol 68 is one of the few in which a statement can return a result. In languages which mix imperative and functional styles, such as the Lisp family, the distinction between expressions and statements is not made: even expressions executed in sequential contexts solely for their side effects and whose return values are not used are considered 'expressions'. In purely functional programming, there are no statements; everything is an expression.

txominpelu
  • 1,067
  • 1
  • 6
  • 11
2

Expressions have a value, while statements do not. If you can pass it as an argument to a function, it's an expression. If you can't, it's a statement. Control flow constructs in C-derived languages are generally statements (you can't pass an 'if {}' block as a function parameter). Consequently, those languages tend to provide an expression form for 'if' (like the ternary operator). Of course, the whole point of many functional languages is that everything has a value, and that functions are first-class values, which means that everything is an expression. Statements usually appear in imperative languages, where you wish to write commands that don't necessarily return a value.For Details see a link.
http://lambda-the-ultimate.org/node/1044
And This Question of StackOverflow also help you.
Expression Versus Statement

Community
  • 1
  • 1
4b0
  • 21,981
  • 30
  • 95
  • 142
0

statements typically do not have a value. expressions typically do have a value.

This is often visible in languages such as Ruby or Erlang that provide an value to something such as if .. end constructs:

> foo = if 1 then "hello" else "goodbye" end
=> "hello"
> foo
=> "hello"
1> Foo = if 1 =:= 1 ->
1>   hello;
1> true ->
1>   goodbye
1> end.
hello
2> 

Many languages will not allow you to use lvalue = if ....

sarnold
  • 102,305
  • 22
  • 181
  • 238
0

Language's "statements" are actually instructions for computer system to do something.

Language's "expressions" are combination of operators and operands like z=x+y. Roughly I can say that the language expressions are mathematical in nature.

Every language's "expression" is a language's "statement" but every language's "statement" is not a language's "expression".

ishaq ahmad
  • 75
  • 1
  • 4
  • 7
  • It's not necessarily true that statements are a subset of expressions; you could imagine a language where just writing `a + b` is illegal without saying `c = a + b`. – Danica May 02 '12 at 18:04