Section 1 - Assignment Statements

 

An assignment statement changes the value of a previously declared variable. It consists of the variable name followed by the equal sign (=) followed by a value. Assignment statements look very similar to variable declarations that contain initialization. The difference is that assignment statements do not specify a type.

int bird = 5;    // Declares variable bird, and initializes it to 5
bird = 6;        // Assigns previously declared variable bird to 6

 

After an assign statement, the memory location associated with the variable contains the value of the literal, expression, variable, or method return specified to the right of the equal sign.

 

                    

 

A variable must be declared before it can be assigned. Note that is does not have to be previously initialized.

dog = 2;        // ERROR: variable dog not declared

int dog;
dog = 2;        // OK

 

The value specified in an assign statement can be a literal, an expression, a variable, or a method return value.

int dog;
int cat = 3;
dog = 2;        // Assign dog to the literal value 2

 

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.