The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Statements vs. Expressions

Can someone explain the difference to me in easy and simple words, please?

I once read a blog entry of Eric Lippert's but I didn't quite grasp the difference. I'll try reading it again. It's an old entry from 2005 I guess.

But can we have a discussion on that in simple words. Someone who understands the difference well and can explain in simple words.
Sathyaish Chakravarthy Send private email
Thursday, April 05, 2007
The exact definitions are language dependent. In Perl for example, statements and expressions are synonyms.

In general, most of the people I work with would consider a statement to be some action that needs to be peformed, and an expression to be a conditional to be evaluated.

IF TRUE THEN /* expression */
  RETURN TRUE /* statement */

However, I've met a lot of people who think there's no difference between them; a statement is an expression and an expression can be either a statement or a conditional.
Thursday, April 05, 2007
I think (I'm not sure) that an expression has a value and can be used on the right-hand-side of an assignment operation.

 void foo() {}
 int bar() {return 1;}

'foo()' is a statement, and 'bar()' is an expression.

'(x == 1)' is an expression of type bool.

In C, 'x = 1' is also an expression (because you can say 'y = x = 1').
Christopher Wells Send private email
Thursday, April 05, 2007
According to this:

An 'expression' evaluates to a single value.  A 'statement' is a broader term.  You can have "Expression statements" which evaulate to a single value, "Declaration Statements" which declare a variable, or "Control Statements" which implement code control structure.

Having designed a compiler or two, I think the word "Expression" is usually used for mathematical calculations, including perhaps function calls that return values.

And I agree with the idea that a "statement" is a very generic term -- like "sentence".
Thursday, April 05, 2007
try this:

statement is a larger unit of instruction
expression is a value which may involve calculation
Thursday, April 05, 2007
I'll throw in my uniformed two cents too.

An expression is a series of tokens which can be evaluated to obtain a value of a specific type.

Thus expressions may contain expressions. 

E.g.  1
    (1 + 1)
    (1 + (1 + 1)) 

Notice that (1 + (1 + 1)) contains the expression (1 + 1).

Statements are independent units of code.  An expression can be a statement in many languages.  But a statement is not necessarily an expression.

The best demonstration I have is using C:

if (true) { 1; } else { 2; }  is a statement.  It contains three expressions (true, 1, 2), two of which are also statements (1, 2).  It does not evaluate to a type.

Conversly, (true) ? 1 : 2; is an expression (and a valid statement).  The result evaluating the expression is an integer.

The most important thing is that expressions have types.  This fact is fairly constant.  Statements are a logical organization of code, and language dependent.

Thursday, April 05, 2007
Just to add some redundancy to the things that have been said...

expression ~ value: a space in memory representing some data type (generally used to being assigned to a variable or used to become part of a new expression). The idea is this expression has to be *used* by someone (it's the expression needed in a sentence or it's used by other expression that contains it).

(expression can be just a value or a combination of other expressions using operators: but when it will be evaluated the calculated result will be a single value)

statement ~ sentence ~ instruction: it performs some action (call of some function/routine, operators like +/-*, etc.).

Note: Depending on the language/instruction itself, the result of this action may be a value, so can be seen as an expression. Example: function calls.

In C a lot of sentences (not all) may be used as expressions AND expressions are valid as sentences too (adding ; at the end), this may result confusing to beginners. Other languages are more restrictive, for example Visual Basic. Let's see some examples.

Visual Basic
num = 2007    '<- this is a sentence (allocation).
(Here '2007' is a numeric expression, integer type, I guess)

if a = 5 then    '<- this is a sentence (if) (all the block).
('5' is a numeric expression, Integer type, I guess.
'a' is a variable, so here it's seen as an expression of its type
'a = 5' is an expression, Boolean type, '=' is the comparison operator)

a = 5;     <- this is a sentence (allocation).
(5 is an integer expression here)

if (a == 4) ... <- this is a sentence (if).
(a==4 is a boolean expression here, 'a' and '4' are the expressions needed by the comparison operator '==')

C Example of sentence used as expression
c=3;  <-- sentence (allocation).

But in C allocation may be used as an expression: its value & type will be the ones of the variable being allocated (c in this case).

if (alfa == (c=3) ) <-- sentence (if) with other sentence embedded: "c = 3" is executed and the value resulting of c is used as expression in the comparison (alfa == c)

COBOL Example of sentence used as expression (unsuccessful)
COMPUTE C = 3.  <-- sentence (allocation)

IF ALFA = COMPUTE C = 3  <- ERROR, 'COMPUTE C = 3' is not an expression.

VB Example of sentence used as expression (unsuccessful)
c = 3  <-- sentence (allocation)

if alfa = (c = 3) Then <- ERROR, '(c = 3)' is not an integer expression for the VB compiler*

So this can not be done in VB or COBOL because the sentence 'allocation' is not reusable as an expression in these languages.

(*) ok, "c = 3" is a Boolean expression because VB uses '=' as comparison operator too, but *the point is* that VB does not see an allocation sentence here in 'c = 3'

C Example of expression used as a sentence
1 + 2;
1 == 2;    <-- valid sentence

VB Example of expression used as a sentence (unsuccessful)
1 + 2      <-- "Syntax Error"
1 == 2    <-- "Syntax Error"
Ross Sampere Send private email
Thursday, April 05, 2007
I've always taken expression to mean "something which returns a value" (as AllanL5 said).

So the following are expressions:

Whereas a statement is... um, "something which doesn't return a value". Statements may contain other expressions, or statements. You'll need a sanit- eh, the Wikipedia page contains a decent list of where you draw the line on how big "something" is.

So the following are statements:

Wikipedia has some useful stuff:
Sunday, April 08, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz