Contents | Prev | Next | Index


Assignments

The purpose of an assignment is to replace the current value of a variable with a new value specified by an expression. The expression must be assignment compatible with the variable. The assignment operator is written as ":=" and stands for becomes.

assignment      ::= left_designator := expr
left_designator ::= designator

If an expression e of type Te is assigned to a variable v of type tv, the following happens:

  1. If Tv and Te are record types, only those fields of Te are assigned which also belong to Tv (projection); the dynamic type of v must be the same as the static type of v and is not changed by the assignment.
  2. If Tv and Te are pointer types, the dynamic type of v becomes the dynamic type of e.
  3. If Tv is an ARRAY [0..n-1] OF CHAR and e is a string of length m<n, v[i] becomes ei for i=0..m-1 and v[m] becomes 0C.

The following are examples of assignments (refer to examples in the section about variable declarations):

i := 0
p := i=j
x := i+1
k := log2(i+j) (* see procedure declaration examples *)
F := log2 (* see procedure declaration examples *)
s := {2,3,5,7,11,13}
a[i] := (x+y) * (x-y)
t^.key := i
w[i+1].name := "John"
t := c


Contents | Prev | Next | Index

Canterbury Modula-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com