Skip to main content

Section 3.1 Convert Temperature

Subgoals for writing expressions:.

  1. Craft name of variable
  2. Determine operators, function calls, or method calls that will produce the value of variable
  3. Decide order of operands and operators
    1. Operators and operands must be compatible
    2. Decompose as necessary

Subsection 3.1.1

Problem: Write an expression that will convert the float variable fahrenheit to be in degrees celsius instead.

Subsection 3.1.2 1. Craft name of variable

Since the original variable was named fahrenheit, a corresponding name for the result variable might be celsius.
celsius = ___

Subsection 3.1.3 2. Determine operators, function calls, or method calls that will produce the value of variable

The formula for converting fahrenheit to celsius is:
Subtract 32 (-) Multiply by 5 (*) Divide by 9 (/)
Therefore, we will use those three operators.

Subsection 3.1.4 3. Decide order of operands and operators

The subtraction must occur first, before the multiplication, so we wrap that part of the expression in parentheses.
(fahrenheit - 32)
The multiplication and division have the same priority and are transitive, so we can leave them in the regular order.
(fahrenheit -32) * 5 / 9
Answer.
celsius = (fahrenheit -32) * 5 / 9
You have attempted of activities on this page.