Using Variables and Operators
The first thing we have to do after creating variables is giving it a initial value. In most language, the way we do this is using the variable name and then equal sign your initial value.
int score;
score = 0
Word 'score' now have initial value of 0. In programming, the equal sign can be called assignment operator. Operator itself means a shortcut to perform a specific task (one operation), like adding or subtracting.
- Addition operator (+) for adding variable
- Subtraction operator (-) for subtracting variable
- Multiplication operator (*) for multiplying variable
- Division operator (/) for dividing variable
score + 10
score - 10
score * 10
score / 10
As you can see, it's very similar to basic math. All these operator need two variable to work, something on the left and something on the right. These operator called binary operators.
Back to assignment operator, variable will assigned the new value from the left side of equal sign. It doesn't have to be just one number. You can assign any result of calculation. You can even assign variable to another variable as long as still the same type.
int score
score = 10 //score is now 10
score = 10 + 20 //score is now 30
int newScore
newScore = score //newScore is now 30
You can also combining declaring variable with assigning value in most language.
int score = 0 //score is now 0
score = 10 + 20 //score is now 30