CSCI 150, Spring 2003
Home | | Course Schedule | | Assignments | | Lecture Notes1) Numbers can be added, subtracted, multiplied and divided as you might expect:
Example:
var x, y, z: real; begin x := 2.3; y := 1.2; z := x + y; end;
2) You can change the value of a variable by adding, subtracting, multiplying or dividing it by another number :
z := z + 1; { Add 1 to z } x := x * 2; { Multiply x by 2} y := y - 5; { subtract 5 from y}
3) Precedence:
Multiplication and Division are performed before addition or subtraction:
x := 8 / 2 + 2; {Value of x is 6} y := 2 + 3 * 4; {Value of y is 14}
When multiplication and division are in the same expression, the order is evaluated starting at the left and moving to the right:
z := 4/2 * 2; {division is first, final value is 4}
To change the order of operations, use parentheses:
z := 4/(2 * 2); {Now the multiplication is first, the final value is 1} x := 8/(2 + 2); {Addition comes first, so final value is 2}