Collapse Content Show Content

Now that you know what a variable is, we will clarify a common area of confusion between algebra and programming.

In algebra, you can have a statement like this:

x = 2 + 3

This is a "truth-statement" that the variable x is equal to 2+3 or 5.

Similarly, in programming, you can write int x = 2 + 3 and the variable x will end up as the integer 5. However, there's a big difference between the two statements, as will now be seen.

What if you faced the following two statement together?

x = 2 + 3

x = 11

In algebra, you would immediately recognize that this is impossible. x cannot be both 2+3 and 11 since they're not equal. However, in programming, = is not a statement of truth, it's an action that assigns a value to a variable.

So you can assign 2+3 to x and then afterwards assign 11 to x , and x will now be set to 11 instead of 5.

Equality Check

If you wanted to check if something was equal in programming, you use == . For example:

int x = 2 + 3; //assignment x == 11; //this would be false

Review this: