2 program structure – Rice Lake iRite IDE User Manual

Page 17

Advertising
background image

920i

Programming Reference - Language Syntax

13

To be the best by every measure

When using the divide operator with integers, be careful of losing significant digits. For example, if you are
dividing a smaller integer by a larger integer then the result is an integer zero: 4/7 = 0. If you were hoping to
assign the result to a real like in the following example:

rSlope : real;

rSlope := 4/7;

rSlope will still equal 0, not 0.571428671 as might be expected. This is because the compiler does integer math
when both operands are integers, and stores the result in a temporary integer. To make the previous statement
work in

iRite

, one of the operands must be a real data type or one of the operands must evaluate to a real. So we

could write the assignment statement like:

rSlope := 4.0/7;

If we were dividing two integer variables, we could multiply one of the operands by 1.0 to force the compile to
resolve the expression to a real:

rSlope : real;

iRise : integer := 4;

iRun : integer := 7;

rSlope := (iRise * 1.0) / iRun;

Now rSlope will equal 0.571428671.

Note

The plus sign (+) is also used as the string concatenation operator. The minus sign (–) is also used as a
unary minus operator that has the result equal to the negative of its operand.

Assignment Operator (:=)

The assignment operator is used to assign a value to a compatible program variable or to initialize a constant. The
value on the left of the “:=” must be a modifiable value. The following are some invalid examples:

3 := 1 + 1; -- not valid

ciMaxAge := 67; -- where ciMaxAge was declared with keyword constant

iInteger := "This is a string, not an integer!"; -- incompatible types

Structure Member Operator (“dot”)

The “dot” (.) is used to access the name of a field of a record or database types.

3.2

Program Structure

A program is delimited by a program header and a matching end statement. The body of a program contains a
declarations section, which may be empty, and an optional main code body. The declaration section and the main
code body may not both be empty.

<program>:

program IDENTIFIER ’;’

<decl-section>

<optional-main-body>

end IDENTIFIER ’;’

;

<optional-main-body>:

/* NULL */

| begin <stmt-list>

;

PROGRAM

IDENTIFIER

;

END

IDENTIFIER

;

decl-section optional-main-body

Figure 3-1. Program Statement Syntax

Advertising
This manual is related to the following products: