8.6.1 Sequence
The idea of sequence is illustrated graphically in Figure 8.9. This figure shows the order in which the statements of the program of Figure 8.6 would be executed.
Sequence is the most basic control construct. It is the “glue” that holds the individual statements of a program together. Yet, when students who are new to programming try to understand how a particular program works, they often just glance over the various statements making up the program to get a “feel” for what it does, rather than methodically tracing through the sequence of actions it performs. One reason such an approach is tempting is because students tend to believe they can figure out what a program is “supposed” to do based on contextual clues such as the meaning of variable names and character strings. After all, it doesn’t take a rocket scientist to figure out that the program of Figure 8.4 prints “Hello world!”.
Figure 8.10: A program and variable trace designed to illustrate the concept of sequence
While it is often possible to gain a superficial knowledge of a program simply by reading it, this approach will not give you the kind of detailed understanding that is frequently required to accurately predict a program’s output. Being able to carefully trace through a program to determine exactly what it does is an important skill. Failure to carefully follow the sequence of instructions often leads to confusion when trying to understand the behavior of a program.[4]
Figure 8.10 illustrates the importance of sequence. It contains a little “do nothing” program that prints the value 16 on the display screen. What makes this program interesting is not so much what its output is, as the way in which that output is computed. Without carefully tracing through the program, one statement at a time, it would be difficult to correctly predict the final output generated by the program.
The first three lines of the program define the numeric variables x, y, and z. In Figure 8.10 the state of the program’s memory after executing each line of code is given beside that statement. After performing the first declaration, the program knows only about the number x. After the second declaration, it knows of x and y, and after the third, x, y, and z. Since these variables have not yet been assigned values, their values are considered to be undefined at this point.
The next instruction
x = 5;
places the value 5 into the variable x.
Notice at this point that the values of y and z are still undefined. The next statement:
x = x + 1;
will place a 6 into x. It works in the following way. First, the expression
x + 1
is evaluated. Since the value of x is 5 at this point, the expression is equivalent to 5 + 1 which, of course, evaluates to 6. Now that the value of the right hand expression has been computed, that value can be assigned to the variable on the left hand side of the assignment operator, x. Hence, the current value of x, which was 5, is replaced by 6.
The next instruction:
y = 3;
places a value of 3 into y, leaving only z without a value. This situation is rectified by the next statement:
z = x + y;
which stores a 9 into z. The expression
x + y
generates this value due to the fact that when it is evaluated x has a value of 6 and y has a value of 3, giving 6 + 3 or 9.
The next instruction to be executed:
y = y - 2;
has the effect of subtracting 2 from the current value of y, which was 3, giving 1. Note that this statement has no effect on the values stored in x or z.
The final statement of the program:
document.writeln(x+y+z);
will print the value 16 on the display screen. That value is computed in the following way:
x + y + z
6 + 1 + 9
7 + 9
16
Exercises for Section 8.6.1
1. What would be printed by the following Watson JavaScript program?
Footnotes
[4] And incorrect answers on exams, I might add.