ConditionalIsSequenceDRAFTNot Observed
One if () then else
block is equivalent to sequence of two if () then
blocks.
If-then-else block is equivalent to sequence of two if-then blocks
If-then-else block can behave differently from sequence of two if-then blocks
CorrectionHere is what's right.
An if-then-else
block executes only one of the two bodies:
A sequence of two if-then
-blocks, however, may execute both bodies:
Why? Because of Mutable State!
The value of the condition may change between the two tests. This is because in Scratch the value of variables can change over time.
The if-then-else
block checks the condition only once,
before it executes any of its contained blocks.
With a sequence of two if-then
blocks,
the condition is evaluated twice.
These two scripts can behave differently! The value of the condition changes between the two tests.
OriginWhere could this misconception come from?
SymptomsHow do you know your students might have this misconception?
This misconception can be detected by asking students to draw a control-flow graph.
ValueHow can you build on this misconception?
This misconception might be a could opportunity to discuss mutability.
If Scratch did not support mutable state
(variables that can have different values at different points in time),
only one of the two conditions, c
and not c
, count be true,
even if they were tested at different points in time.
However, Scratch supports mutable state.
Variables and sprites can change state over time.
Thus, at different times
the same expression (i.e., c
) may evaluate to different values.
Thus, if the value of c
changes between the time we evaluate c
and the time we evaluate not c
, both conditions might be true.
There are languages that have no mutable state; in those languages this misconception does not exist.