In the last lesson, we saw that we can represent values in a computer using either variables or constants. Variables are named memory locations whose value can change while a program executes whereas constants are named values which cannot change while a program executes. But how do we tell the computer the value that a variable should contain or that a constant should represent? This is accomplished through the assignment operation.
In many high-level languages like C++ and Java, the assignment operation is done by placing an equal sign between a value and a variable like this:
|PriceOfShoes = 19.95|
In some other languages like Pascal, the assignment operator is a colon with an equal sign like this:
|TAXRATE := 0.045|
For our lessons, we will always use a colon with an equal sign to avoid confusing the assignment operation with a test for equality.
Notice the format of the assignment operations. On the left side of the assignment operator is the name of a variable or constant while the right side has the value to be assigned. We can also place variables and constants on the right side like this:
|OtherPrice := PriceOfOtherShoes|
After this assignment operation is complete, the variable OtherPrice contains a copy of the value stored in PriceOfOtherShoes. We can summarize a general rule for assignment with the following table.
|Left Side||Right Side|
|Variable or CONSTANT||:=||Expression, Variable, or CONSTANT|
Notice that the right side of the table allows us to use something called an expression. In the next lessons we will learn more about these, but for now, just think of an expression as a typical math formula like 3 * 2 or 4 + 1. Below are some more examples of assignment operations.
The following animation reviews the concepts of identifiers, variables, constants and assignment. It also demonstrates the construction and execution of the simple tax program from the first lesson.
[View in New Window]