Computer Science Canada

Understanding Statements and Expressions

Author:  wtd [ Sun Oct 31, 2004 10:02 pm ]
Post subject:  Understanding Statements and Expressions

Two of the most frequently confused terms in programming are statement and expression.

A statement in a program is code which doesn't have any value on its own. In Turing, consider:

code:
var hw : string := "Hello world"


This does something, but it can't be used as a value.

An expression has a value of its own. In an ideal world it wouldn't actually do anything to the rest of the world, but very few languages enforce that. Consider:

code:
var hw : string := "Hello " + "world"


All together this isn't an expression, but rather a statement. However:

code:
"Hello " + "world"


Is an expression. It doesn't modify either "Hello " or "world", but it creates something new and returns it.


: