Information Technology Reference
In-Depth Information
Note A block counts syntactically as a single embedded statement. Anywhere that an embedded state-
ment is required syntactically, you can use a block.
An empty statement consists of just a semicolon. You can use an empty statement at a
position where the syntax of the language requires an embedded statement, but your program
logic does not require any action.
For example, the following code is an example of using the empty statement:
￿
The second line in the code is an empty statement. It is required because there must be
an embedded statement between the if part and the else part of the construct.
￿
The fourth line is a simple statement, as shown by the terminating semicolon.
if( x < y )
; // Empty statement
else
z = a + b; // Simple statement
Expression Statements
The last chapter looked at expressions. Expressions return values, but they can also have
side effects .
￿
A side effect is an action that affects the state of the program.
￿
Many expressions are evaluated only for their side effects.
You can create a statement from an expression by placing a statement terminator (semi-
colon) after it. Any value returned by the expression is discarded. For example, the following
code shows an expression statement. It consists of the assignment expression (an assignment
operator and two operands) followed by a semicolon. This does the following two things:
￿
The expression assigns the value on the right of the operator to the memory location
referenced by variable x . In fact this is probably the main reason for the statement, this
is considered the side effect .
After setting the value of x , the expression returns with the new value of x . But there is
nothing to receive this return value, so it is ignored.
￿
x = 10;
The whole reason for evaluating the expression was to achieve the side effect.
Search WWH ::




Custom Search