Operators/Addition
From FizzFuzz
- Go back to Operators.
Addition is an operation performed on two numeric values. It is indicated by the + operator (the plus sign), used as an associative, commutative, binary infix operator.
Contents |
Notation
α + β;
Where α and β are both numeric (int or float). If αtype ≠ βtype, then the value with the less precise type will be coerced to the value of the more precise type (that is, an int will be coerced to a float, and an unsigned int will be coerced to a signed int, and etc.)
Assignment
Α += β; Α = Α + β;
Where
- Α is the variable being modified (either an int or float).
- β is the variable doing the modification.
The first example is shorthand for the second.
Example
int alpha = 10; int beta = 5; float pi = 3.14159; float rho = 2.71828; output << alpha + beta; // Outputs "15". output << pi + rho; // Outputs "5.85987". output << alpha + pi; // Outputs "13.14159", as alpha is less precise than pi. beta += rho; output << beta; // Outputs "7", beta is first cast as a float, making the result 7.71828. // It's then recast to an int, and loses the integer value. output << pi + beta; // Outputs "9.71828", as beta is cast as a float, becoming 7.0.

