Arithmetic Operators
Arithmetic operators transform data.
A Minus Sign Can Do Two Jobs
Arithmetic operators transform data. Subtraction reduces a quantity by removing the right operand from the left, while multiplication can produce a numeric product or repeat a string. A minus sign also has a second role: when it appears without a left operand, it negates the number that follows.
What do you think happens?
Predict the result of each expression before checking the explanation.
Reveal answer
10 - 4 produces 6, and -4 produces negative 4
In 10 - 4, the minus sign has a left operand, so it removes 4 from 10. In -4, there is no left operand, so the minus sign negates 4.
Tracing Subtraction
Following the operands
Evaluate 18 - 7 and then evaluate 7 - 18.
- 1First expression
In 18 - 7, 7 is removed from the left operand, 18, producing 11.
- 2Second expression
In 7 - 18, 18 is removed from the left operand, 7, producing negative 11.
- 3Compare the order
Changing which value is on the left changes the subtraction result.
18 - 7 produces 11, while 7 - 18 produces negative 11.
For subtraction, always identify the left operand first. The operation is directional: the right operand is removed from the left operand, not the other way around.
Multiplication Across Data Types
When both operands are numbers, multiplication produces a numeric product. When the left operand is a string and the right operand is an integer, multiplication repeats the string. These are different results from the same operator because the operand data types differ.
Reading multiplication by type
Evaluate 6 multiplied by 5 and the string ha multiplied by 4.
- 1Numeric multiplication
Both operands in 6 multiplied by 5 are numbers, so the result is the numeric product, 30.
- 2String multiplication
The left operand is the string ha and the right operand is the integer 4, so the string is repeated four times.
The numeric expression produces 30. The string expression produces hahaha.
Mistakes Beginners Make
Common mistake Reversing the operands in subtraction
What it looks likeTreating 9 - 2 as though it removes 9 from 2
Why it failsSubtraction removes the right operand from the left operand.
Do this insteadRead 9 - 2 as 9 with 2 removed.
Common mistake Treating unary minus as subtraction
What it looks likeReading -8 as an operation between two values
Why it failsThere is no left operand in -8, so the minus sign negates 8.
Do this insteadIdentify whether a value appears to the left of the minus sign.
Common mistake Using a non-integer multiplier with a string
What it looks likeMultiplying a string by a float
Why it failsString multiplication requires the right operand to be an integer.
Do this insteadUse an integer multiplier when repeating a string.
Common mistake Multiplying two strings
What it looks likeUsing a string as both operands of multiplication
Why it failsThe supported string repetition pattern has a string on the left and an integer on the right.
Do this insteadUse numeric operands for a numeric product, or place an integer multiplier to the right of the string.
When an expression produces an unexpected result, trace it in a fixed order: identify each operand, determine its data type, classify the minus sign as unary or binary, and then check whether multiplication is producing a product or repeating a string. Recording the variable state and output at each step helps verify a prediction and locate an incorrect assumption.
Practice Before Execution
Predict the result of each expression, then explain which operator rule you used: 14 - 9, -6, 7 multiplied by 3, and the string go multiplied by 3.
Hints
- For subtraction, remove the right operand from the left operand.
- For a minus sign with no left operand, negate the following number.
- For multiplication, check whether both operands are numbers or whether the left operand is a string and the right operand is an integer.
Practice check
Evaluate 14 - 9, -6, 7 multiplied by 3, and the string go multiplied by 3.
- 1Subtraction
Remove 9 from 14 to get 5.
- 2Unary minus
There is no left operand, so -6 represents the negated value of 6.
- 3Numeric multiplication
Both operands are numbers, so 7 multiplied by 3 produces the numeric product 21.
- 4String multiplication
The string go is followed by the integer 3, so it is repeated three times.
The results are 5, negative 6, 21, and gogo go without a space between repetitions: gogogo.
Operator Rules to Remember
- 1Binary subtraction removes the right operand from the left operand.
- 2Unary minus has no left operand and negates the number that follows.
- 3Multiplication produces a numeric product when both operands are numbers.
- 4A string can be repeated when it is the left operand and an integer is the right operand.
- 5Execution traces that record variable state and output help you check predictions and debug mistakes.
Key Takeaways
- Subtraction is directional: the right operand is removed from the left operand.
- A minus sign without a left operand is unary minus and negates the following number.
- Multiplication produces a numeric product for two numbers.
- Multiplication repeats a string when the string is on the left and an integer is on the right.
- Checking operand order and data types is the fastest way to diagnose these operator mistakes.