Skip main navigation

Assignment operator

Find out what the assignment operator is and how it is used
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.
This article is from the free online

Begin Programming: Build Your First Mobile Game

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education