Arithmetic Operators
Subtraction removes the right operand from the left; unary minus (a single minus with no left operand) negates a number.
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
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.
First expression: In 18 - 7, 7 is removed from the left operand, 18, producing 11.
Second expression: In 7 - 18, 18 is removed from the left operand, 7, producing negative 11.
Compare 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.
Numeric multiplication: Both operands in 6 multiplied by 5 are numbers, so the result is the numeric product, 30.
String 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
Reversing the operands in subtraction
Subtraction removes the right operand from the left operand.
Fix:
Read 9 - 2 as 9 with 2 removed.Treating unary minus as subtraction
There is no left operand in -8, so the minus sign negates 8.
Fix:
Identify whether a value appears to the left of the minus sign.Using a non-integer multiplier with a string
String multiplication requires the right operand to be an integer.
Fix:
Use an integer multiplier when repeating a string.Multiplying two strings
The supported string repetition pattern has a string on the left and an integer on the right.
Fix:
Use 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.
Subtraction: Remove 9 from 14 to get 5.
Unary minus: There is no left operand, so -6 represents the negated value of 6.
Numeric multiplication: Both operands are numbers, so 7 multiplied by 3 produces the numeric product 21.
String 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
- Binary subtraction removes the right operand from the left operand.
- Unary minus has no left operand and negates the number that follows.
- Multiplication produces a numeric product when both operands are numbers.
- A string can be repeated when it is the left operand and an integer is the right operand.
- Execution 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.