Many times a programmer is given an algebraic formula, and must write an expression that will correctly calculate the answer. After writing a statement, check it by hand, using sample values and the method shown above to verify that the equation is correct. Use the following guidelines to convert algebraic expressions to programming statements:
| Algebra | Program | Explanation: | |
| xy | X*Y | Algebra uses single letters for variables, and can omit the *, programming languages always require an operator. |
|
| x·y | X*Y | Algebra sometimes uses a dot for multiplication. |
|
| X^2 | Programming languages use only one line for equations: they can not write anything above or below the line. |
||
| X^Z | The ^ operator is used for any power |
||
| Sqrt(X) | The System.Math.Sqrt function returns the square root |
||
| (A+B)/C | Compound numerators and divisors must be enclosed in parenthesis: Notice the difference between this example and the one below: |
||
| A+B/C | Notice the difference between this example and the one above. |
We will learn about using variables in the next lesson.