Assignment statements |
index |
| BASIC |
An assignment statement gives a value to a variable. For example,
x = 5;
gives x the value 5.
The value of a variable may be changed. For example, if x has
the value 5, then the assignment statement
x = x + 1;
will give x the value 6.
The general syntax of an assignment statement is
variable = expression;
where:
variable must be declared;variable may be a simple name, or an indexed location
in an array, or a field (instance variable)
of an object, or a static
field of a class; andvariable. In other words, it must be possible
to cast the expression to the type of the variable. Examples:
int i = 0;
price[itemNumber] = 0.80 * price[itemNumber];
myDog.name = "Fido";
| INTERMEDIATE |
An assignment "statement" is not really a statement (although it is typically used that way), but is an expression. The value of the expression is the value that is assigned to the variable. For example, the expression
i = j = k = 0;
sets all of i, j, and k to zero.