Operators

Operators are the symbols used to work with variables. You’re already familiar with operators from simple arithmetic; plus and minus are operators. See Table 1.3 for the full list of operators.

Table 1.3. Operators
OperatorWhat it does
x + y (Numeric)Adds x and y together
x + y (String)Concatenates x and y together
x - ySubtracts y from x
x * yMultiplies x and y together
x / yDivides x by y
x % yModulus of x and y (i.e., the remainder when x is divided by y)
x++, ++xAdds one to x (same as x = x + 1)
x--, --xSubtracts one from x (same as x = x - 1)
-xReverses the sign on x

✓ Tips

  • While both x++ and ++x add one to x, they are not identical; the former increments x after the assignment is complete, and the latter before. For example, if x is 5, y=x++ results in y set to 5 and x set to 6, while y=++x results in both x and y set to 6. The operator–- (minus sign) works similarly.

  • If you mix numeric and string values when adding two values together, the result is a string. For example, cat + 5 results in cat5.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.101