Practice question · Put in order
Order what the machine does when it runs the single line x = x + 1, given that x currently holds 7.
- From now on the name x refers to 8
- Add 1 to that value, giving 8
- Store 8 back into x, overwriting the old value
- Read the current value of x, which is 7
Hints
- The right-hand side is fully evaluated before any storing happens.
- The old value is read before the new value is written, so there is no contradiction.
Show the answer
- Read the current value of x, which is 7
- Add 1 to that value, giving 8
- Store 8 back into x, overwriting the old value
- From now on the name x refers to 8
Why
Read, compute, store, then the name carries the new value. Because the read happens before the write, x = x + 1 is a command and not a false equation, the very point this lesson insists on.
Practise Variables, Expressions, and Types
The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.