An operator takes one or more value and combines them in some way to produce a new value.
In general, we will use operators with two different items
Variables
constants or values that are hard coded into the program.
There are two types of constants
Literal constants or values that are included directly into the code.
Named constants, or a value that has an associated identifier.
The assignment operator has two operands
On the left hand side (LHS) is a destination, which must be a variable
On the right hand side (RHS) is an expression
Syntax lhs = rhs
An expression is a combination of values, constant, literals, variables, operators and possible function calls such that when evaluated produce a resulting value.
Expression examples
10
principal * rate * time
radius * cos(angle)
Statement example
age = 10
interest = principal * rate * time
x = radius * cos(angle)
The assignment operator
Evaluates the expression on the RHS
Sets the variable on the LHS equal to the new value.
If we model memory as a follows:
The expression y = 3*x + 7
Gets the value 4 stored in memory associated with x
Evaluates the RHS to 3*4 + 7 -> 19
Then places the 19 in the memory associated with y
Produces
Note, you must have a variable on the LHS,
12 = 2*x+7
Is an illegal statement.
Mathematical operators
+, -, and * (for multiply) work as you expect
/ depends on the type of the expression.
% for integers computes the remainder.
For strings
+ usually means concatenation
"Hello" + "World" will produce "HelloWorld"
Please remember my dear aunt sally.
Use () when needed to change this.
Some languages support
a += b which is the same as a = a + b
a *= b which is the same as a = a * b
a++ which is the same as a = a + 1 but be careful in assignments.