Expressions | Statements | Blocks
August 15, 2010 Leave a comment
From a stylistic perspective, an expression is like a phrase; a statement is like a complete sentence; and a statement block is like a paragraph. For example, the following is an expression:
2+3
This code does not do anything other than calculates a result. The following is a statement:
x=2+3;
This code not only calculates a result, it also assigns the result to a variable. Just like a sentence represents a complete thought, a statement represents a complete action. Finally, the following code is a statement block:
{
x=2+3;
y=x;
}
From a syntax perspective, the difference between these three constructs is more basic. Quite simply, a statement is an expression that is followed by a semicolon, and a statement block is a list of statements enclosed in braces. Usually you create expressions, statements, and blocks that follow the phrase/sentence/paragraph parallel; this makes your code easy to read. However, you need to understand the definitions of expression and statement from a syntax perspective.
Expressions
A simple expression is nothing more than a function call. This can be two data items joined by an operator; it can be a call to a primitive; or it can be a call to a user-defined function. For example, each of the following is a simple expression:
2*3
x+y
5!
A compound expression is just like a simple expression except that one or more of the data items is another expression. For example, 2*3 and 4*5 are both simple expressions, so 2*3+4*5 is a compound expression. There is no practical limit to how long a compound expression can be. Here are some examples:
2*3/(x+y)
Anywhere you can enter a simple expression, you can also enter a compound expression. Since there is no real need to draw a distinction between these types, we will refer to both simple and compound expressions as just expressions.
Statements
A statement is nothing more than an expression followed by semicolon (;). For example, x=2+3 is an expression and x=2+3; is a statement.
You probably remember a few paragraphs back I said that the code x=2+3 represents a complete action so it is a statement. To be more specific, we should say that because this code represents a complete action, you could choose to enter it as a statement. However, it is not a statement without the terminating semicolon. As an analogy, consider the following phrase:
John went to the store
If you add a terminating period (.), then this phrase becomes a sentence. Without the period, however, this is just a phrase and can be used as part of a longer sentence such as:
Mike and John went to the store.
To follow this analogy, x=2+3 is an expression, x=2+3; is a statement, and y=x=2+3 is a longer statement using the initial expression.
Statement Blocks
A statement block is two or more statements enclosed in braces { }. For example, the following is a statement block:
{
x=2+3;
y=x;
}
Note that a statement block does not end in a semicolon.
When a statement block is executed, DScript first creates a new block scope (to keep variables local), and then executes each statement in the order listed. Finally, the value of the last statement is returned.
Wherever you can enter a single statement you can instead enter a statement block.
Compound Statements
A compound statement is a sequence of reserved words that combine expressions and statements into a logical group that performs a specific task.
For example, the if compound statement has the format
if( expression )
statement
Although there are numerous ways you can combine compound statements to create a complete application, there are only a small number of distinct statement types.
Recent Comments