Concepts / Arithmetic Operators

Arithmetic Operators

Arithmetic operators transform data.

  • Programming

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.

  • 10 - 4 produces 6, and -4 produces negative 4

  • 10 - 4 produces negative 6, and -4 produces 4

  • Both expressions produce 6

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

12 - 5left operand and rightoperandRemove 5from 127resultread the operandscalculate
What happens first, and what output is produced as a subtraction expression is evaluated step by step?
Worked example

Following the operands

Evaluate 18 - 7 and then evaluate 7 - 18.

  1. 1
    First expression

    In 18 - 7, 7 is removed from the left operand, 18, producing 11.

  2. 2
    Second expression

    In 7 - 18, 18 is removed from the left operand, 7, producing negative 11.

  3. 3
    Compare the order

    Changing which value is on the left changes the subtraction result.

Result

18 - 7 produces 11, while 7 - 18 produces negative 11.

Key point

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

3 × 4both operands are numbersha × 3string followed by integer12numeric producthahaharepeated stringmultiplyrepeat
How does multiplication behave differently when the operands are numbers versus when the left operand is a string?

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.

Worked example

Reading multiplication by type

Evaluate 6 multiplied by 5 and the string ha multiplied by 4.

  1. 1
    Numeric multiplication

    Both operands in 6 multiplied by 5 are numbers, so the result is the numeric product, 30.

  2. 2
    String multiplication

    The left operand is the string ha and the right operand is the integer 4, so the string is repeated four times.

Result

The numeric expression produces 30. The string expression produces hahaha.

Mistakes Beginners Make

  • Common mistake

    Reversing the operands in subtraction

    What it looks like

    Treating 9 - 2 as though it removes 9 from 2

    Why it fails

    Subtraction removes the right operand from the left operand.

    Do this instead

    Read 9 - 2 as 9 with 2 removed.

  • Common mistake

    Treating unary minus as subtraction

    What it looks like

    Reading -8 as an operation between two values

    Why it fails

    There is no left operand in -8, so the minus sign negates 8.

    Do this instead

    Identify whether a value appears to the left of the minus sign.

  • Common mistake

    Using a non-integer multiplier with a string

    What it looks like

    Multiplying a string by a float

    Why it fails

    String multiplication requires the right operand to be an integer.

    Do this instead

    Use an integer multiplier when repeating a string.

  • Common mistake

    Multiplying two strings

    What it looks like

    Using a string as both operands of multiplication

    Why it fails

    The supported string repetition pattern has a string on the left and an integer on the right.

    Do this instead

    Use numeric operands for a numeric product, or place an integer multiplier to the right of the string.

Best practice

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

EASY

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.
Worked example

Practice check

Evaluate 14 - 9, -6, 7 multiplied by 3, and the string go multiplied by 3.

  1. 1
    Subtraction

    Remove 9 from 14 to get 5.

  2. 2
    Unary minus

    There is no left operand, so -6 represents the negated value of 6.

  3. 3
    Numeric multiplication

    Both operands are numbers, so 7 multiplied by 3 produces the numeric product 21.

  4. 4
    String multiplication

    The string go is followed by the integer 3, so it is repeated three times.

Result

The results are 5, negative 6, 21, and gogo go without a space between repetitions: gogogo.

Operator Rules to Remember

  1. 1
    Binary subtraction removes the right operand from the left operand.
  2. 2
    Unary minus has no left operand and negates the number that follows.
  3. 3
    Multiplication produces a numeric product when both operands are numbers.
  4. 4
    A string can be repeated when it is the left operand and an integer is the right operand.
  5. 5
    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.

Related concepts

More concepts starting with A