Assignment

Problem

Teaching assignment of values to variables to new comers to programming may be a bit tricky at first, specially when you come to the “x = x + 3″ part.

Students who have long known the ‘=’ equals sign as used in algebra wold frown at the above ‘equation’ and might have a hard time understanding it or accepting it.

Solution

The trick is to introduce the concept of assignment to students in a way that makes them understand it as method of filling values into variables and not creating an algebraic equation. In order to achieve this, two things should be used.

First of all, always refer to the equals sign as the “assignment” sign. For instance, when writing the code “x = 5″ read it as follows: “Ex assignment five” and never read it as “Ex equals five”. This method would help students avoid thinking of the equals sign in terms of algebraic equations.

Secondly, when explaining the concepts of variables and assignment, draw a one-column table on the whiteboard to model the computer memory with each row representing some variable. While tracing the flow of code in a program, show them how the value in each variable changes. This would turn out to be handy when explaining to them the “x = x + 3″ assignment operation.

Additional Benefit

The practice of always referring to the equals sign as “assignment” when reading it to students and asking them to do the same whenever it is used in an assignment statement has an further benefit. When you start teaching relational operators to students and get to the “==” equality operator, the possibility of them falling into the mistake of using a single equals sign “=” when they actually want to do a relational equality is greatly reduced. In contrast, students who are used to refer to the assignment operator “=” as “equals” often fall into the trap of using a single “=” equals sign during a relational equality comparison mistakenly writing “if(x = 4)” instead of “if(x == 4)”. The human mind is closely tied to linguistic performance, each strongly affects the other.