Delphi Programming/Arithmetic expression

From Wikibooks, open books for an open world
Jump to: navigation, search

An arithmetic expression returns a numerical value that can be used directly or stored in a numerical variable.

Addition operator "+"[edit]

The operator allowing the addition is the + operator. It allows to add a number or the result of an arithmetic expression to a number or an expression.

  1. var
    
  2.   x, y, z: integer;
    
  3.  
    
  4. begin
    
  5.   x := 5; // Value of x is 5
    
  6.   y := 3; // Value of y is 3
    
  7.   z := x + y; // Z now has the value 8 (value of x plus y)
    
  8. end.
    

Subtraction operator "-"[edit]

The operator allowing the subtraction is the - operator. It allows to subtract a number or the result of an arithmetic expression to a number or an expression.

  1. var
    
  2.   x, y, z: integer;
    
  3.  
    
  4. begin
    
  5.   x := 5; // x = 5
    
  6.   y := 3; // y = 3
    
  7.   z = x - y; // Z = 2 (value of x minus y)
    
  8. end.
    

Multiplication operator "*"[edit]

The operator allowing the multiplication is the * operator. It allows to multiply a number or the result of an arithmetic expression with a number or an expression.

  1. var
    
  2.   x, y, z: integer;
    
  3.  
    
  4. begin
    
  5.   x := 5; // x = 5
    
  6.   y := 3; // y = 3
    
  7.   z := x * y; // Z = 15 (value of x times y)
    
  8. end.
    

Division operator "/"[edit]

The operator allowing the division is the / operator. It allows to divide a number or the result of an arithmetic expression by a number or an expression. The result is a float.

  1. var
    
  2.   x, y: integer;
    
  3.   z: real;
    
  4.  
    
  5. begin
    
  6.   x := 5; // x = 5
    
  7.   y := 3; // y = 3
    
  8.   z := x / y; // Z = 1.666 (value of x divided by y)
    
  9. end;
    

Euclidean division operator "div"[edit]

The operator allowing the euclidean division is the div operator. It allows to divide a number or the result of an arithmetic expression by a number or an expression. The result is a rounded integer.

  1. var
    
  2.   x, y, z: integer;
    
  3.  
    
  4. begin
    
  5.   x := 5; // x = 5
    
  6.   y := 3; // y = 3
    
  7.   z := x div y; // Z = 1 (value of x divided by y)
    
  8. end;
    

Modulo operator "mod"[edit]

The operator allowing the modulo is the mod operator. It allows to get the remainder of a number or the result of an arithmetic expression divided by a number or an expression.

  1. var
    
  2.   x, y, z: integer;
    
  3.  
    
  4. begin
    
  5.   x := 5; // x = 5
    
  6.   y := 3; // y = 3
    
  7.   z := x mod y; // Z = 2 (remainder of x divided by y)
    
  8. end;
    

Multiple expression[edit]

You can nest expressions but don't forget the brackets.

  1. var
    
  2.   w, x, y, z: integer;
    
  3.  
    
  4. begin
    
  5.   w := 10; // w = 10
    
  6.   x := 5; // x = 5
    
  7.   y := 3; // y = 3
    
  8.   z := (w - x) * z; // Z = 15 (w subtracted by x and then multiplied by z)
    
  9. end;