Lesson goal: One line changing a variable

Previous: Distance formula with variables | Home | Next: Rescaling a result

In this lesson, you learned how a single = sign is used to assign a value to a variable. What is on the right side of the = sign is what gets put into the variable on the left side. In this context, the = sign is called the "assignment operator," since it makes value assignments to variables. (That is, it assigns the stuff on its right side to the variable on its left side.)

There are very common lines in programming that might look like this, for example in dealing with a variable called x:

x=x+1

Do you now what this line does? It is a strange line because mathematically, it is impossible. There's no way that $x$ can ever be equal to $x+1$, but this is the way it looks: $x=x+1$.

When seeing lines like this, you have to remember the two step process of the = sign. First it goes to the right side of itself and evaluates what's there. If there's an $x+1$ there, it'll take the current value of $x$ and add $1$ to it. When it has this result, then it'll send it into the variable on the left of it. If it happens to be $x$ again, then so be it. In this case $x$ will get reassigned its old value $+1$.

Now you try. Predicting what each print statement will display.

Type your code here:


See your results here: