sum = 42 - 12 ;

Answer:

Yes, the statement is syntactically correct.

Assignment Statement Symantics

The syntax of a programming language says what programs look like. It is the grammar of how to arrange the symbols. The symantics of a programming language says what the program does as it executes. It says what the symbols mean.

This page explains the symantics of the assignment statement. An assignment statement asks for the computer to perform two steps, in order:

  1. Evaluate the expression (that is: calculate a value.)
  2. Store the value in the variable.

For example, the assignment statement:

sum = 32 + 8 ;

asks for two actions:

  1. Evaluate the expression: 32 + 8 is calculated, yielding 40.
  2. Store the value in the variable: 40 is placed in sum

Note (May, 2002): I've just spent several hours grading student programs from second semester computer science. Several programs were not working because their authors forgot that these two steps happen one after the other. Without this concept, an easy program became difficult.

QUESTION 12:

What does the following assignment statement statement do:

sum = 42 - 12 ;

(What are the two steps, in order?)