up previous next
1.1.4 Tutorial: variables, assignment
|
CoCoA-5 includes its own imperative programming language.
Values you plan to use in future computations need to be stored in
variables; the act of storing a value in a variable is also called
assignment. CoCoA-5 uses the colon-equals operator to indicate
assignment, for instance
A := 13;
assigns 13 to the variable
A
.
A variable name must start with a letter, and may contain letters,
digits, and the underscore character. We recommend using names
which are mnemonic (but hopefully not too long).
The most basic types in CoCoA-5 are integers, rationals and strings.
A number written in "decimal notation" is automatically converted to
a rational number: for example
3.14
is converted into the fraction
157/50
.
/**/ A := 1; // assign the integer 1 to the variable "A"
/**/ half := 1/2; // assign rational 1/2 to the variable "half"
/**/ A := 0.333; // assign the rational 333/1000 to "A"
// The previously stored value is overwritten.
/**/ mesg1 := "hi!"; // assign the string "hi!" to variable "mesg1"
|