Jun 17, 2014

C Programming #15: Expression and Statement

Following article formally introduces to the words expression and statement. By now we have used several expressions and statements in example program. I want just to point out what are those so that it is added to your vocabulary, and further explanation can be done better.


Expression

Definition: Expression smallest unit of computation.

When variables/constants are used with operators they form expression. Each expression is evaluated to value which is called value of expression.

For Example
  1. a + 5 + b   -> It is expression that glues variable a and b constant 5 with addition operator.
  2. 4 ^ 3 + 2   -> It is expression that glues constants 4 3 and 2 with XOR and addition operator.

NOTE that there is no semicolon needed at the end of expression.

In 2nd example is 4 ^ 3 done first ? or 3 + 2 is done first ? To know this we need to know Precedence of operator.  Precedence of operator tells priority of operators. This will be taken in next article.

Statement

Definition: Statement is smallest independent unit in C Program.

For example

a = b + 2;

Above C code is a statement. It is statement that contains expression b + 2 and also result assigned to a. Generally statement ends with semicolon, while expression do not.

Expressions are like idioms and phrases while statement are like sentences. These two words will be used a lot going forward.

Links

Quiz - Not Yet written !!
Next Article - C Programming #16: Operator precedence, associativity and order of evaluation
Previous Article -  C Programming #14: Operators - Comma
All Article - C Programming

No comments :

Post a Comment