Rice Lake iRite IDE User Manual

Page 13

Advertising
background image

920i

Programming Reference - Tutorial

9

25

26

g_rRadius := g_iCount;

27

g_rArea := CircleArea(g_rRadius);

28
29

g_sPrintText := "The area of a circle with radius " + RealToString(g_rRadius, 4, 1)

30

+ " is " + RealToString(g_rArea, 7, 2);

31
32

WriteLn(g_ciPrinterPort, g_sPrintText);

33
34

end loop;

On line 23 we see a for loop to start the first statement in the startup handler. In

iRite

there are two kinds of

looping constructs. The for loop and the while loop. For loops are generally used when you want to repeat a
section of code for a predetermined number of times. Since we want to calculate the area of 10 different circles,
we chose to use a for loop.

For loops use an optional iteration clause that starts with the keyword for followed by the name of variable,
followed by an assignment statement, followed by the keyword to, then an expression, and finally an optional
step clause. Our example doesn’t use a step clause, but instead uses the implicit step of 1. This means that lines
26 through 32 will be executed ten times. The first time g_iCount will have a value of 1, and during the last
iteration, g_iCount will have a value of 10.

All looping constructs (the for and the while) start with the keyword loop and end with the keywords end loop,
followed by a semicolon. In our example, loop is on line 24 and end loop is on line 34. In between these two, are
found, the statements that make up the body of the loop.

Line 26 is an assignment of an integer data type into a real data type. This line is unnecessary and the assignment
could have been made automatically if the integer g_iCount was passed into the function CircleArea directly on
line 27, since CircleArea is expecting a real value. Calls to functions like CircleArea are usually done in an
assignment statement if the functions return value need to be used later in the program. The return value of
CircleArea (the area of a circle with radius g_rRadius) is stored in g_rArea.

The assignment on lines 29 and 30 uses two lines strictly for readability. This single assignment statement does
quite a bit. We are trying to create a string of plain English text that will say: “

The area of a circle with

radius xx.x is yyyy.yy

”, where the radius value will be substituted for

xx.x

and the calculated area will

be substituted for

yyyy.yy

. The global variable g_sPrintText is a string data type. The constants (or literals):

The area of a circle with radius

” and “

is

” are also strings.

However, g_rRadius and g_iArea are real values. We had to use a function from the API to convert the real
values to strings. The API function RealToString is passed a real and a width integer and a precision integer. The
width parameter specifies the minimum length to reserve in the string for the value. The precision parameter
specifies how many places to report to the right of the decimal place. To concatenate all the small strings into one
string we use the string concatenation operator, “+”.

Finally, we want to send the new string we made to a printer. The Write and WriteLn procedures from the API
send text data to a specified port. Earlier in the program we decided the printer port will be stored in
g_ciPrinterPort. So the WriteLn call on line 32 send the text stored in g_sPrintText, followed by a carriage return
character, out port 2.

If we had a printer connected to port 2 on the

920i

, every time the program startup handler is fired, we would see

the following printed output:

The area of a circle with radius 1.0 is 3.14

The area of a circle with radius 2.0 is 12.57

The area of a circle with radius 3.0 is 28.27

The area of a circle with radius 4.0 is 50.27

The area of a circle with radius 5.0 is 78.54

The area of a circle with radius 6.0 is 113.10

The area of a circle with radius 7.0 is 153.94

The area of a circle with radius 8.0 is 201.06

The area of a circle with radius 9.0 is 254.47

The area of a circle with radius 10.0 is 314.16

Advertising
This manual is related to the following products: