Want to keep learning?

This content is taken from the University of Reading's online course, Begin Programming: Build Your First Mobile Game. Join the course to learn more.

Assignment operator

The assignment operator is used to assign values to a variable called an ‘operand’ (we introduced you to this operator through examples in the last step).

For example:

float mBallX; //declares mBallX to be of type float
mBallX = 0;   //assigns a value of 0 to mBallX

After the execution of above statement the value of the variable mBallX will be 0. That is, the assignment operator has assigned a value of 0 to mBallX.

In order to assign a value of 100 to mBallX, you simply need to change the value that comes after the operator.

mBallX = 100;   

When you assign a value it’s important to check that it fits within the range the data type accepts.

For example, the byte data type can only hold values with a minimum value of -128 and a maximum value of 127, so you are not able assign a value of 345 or -6892 to a variable of this data type.

You can check which data type is most appropriate for a particular use in Step 2.4.

Share this article:

This article is from the free online course:

Begin Programming: Build Your First Mobile Game

University of Reading