Programs can process data in a variety of ways. They may, for example, perform calculations with numbers, or save and recall strings of text (such as names and phone numbers in a data file).
In all cases, your program must be able to handle values different types of numbers, strings, and so on.
In OPL, there are two ways of handling values: variables and constants. Constants are fixed values, such as 1, 2, 3. Variables are used to store values which may change for example, a variable called "X" may start with the value 3 but later take the value 7.
Most procedures begin by declaring (creating) variables:
LOCAL is the word telling the Series 3a to create variables, with the names which follow here "x", "y" and "z" separated by commas.
The statement "LOCAL x,y,z" defines three variables called "x", "y" and "z". The Series 3a will recognise these names whenever you use them in this procedure. (If you used them in another procedure, they wouldn't be recognised; the variables are `local' to the procedure in which they are declared.)
These variables are initially given the value 0.
Any variables you wish to use must be declared at the start of a procedure.
Before declaring variables, decide what information they are going to contain. There are different types of variables for different sorts of values. If you try to give the wrong type of value to a variable, an error message will be displayed.
You specify the type of each variable when you declare it, by adding a symbol at the end of its name.
For text "Are you sure?", "54th", etc. use a string variable. (Pieces of text are called strings in OPL.) String variables have a "$" symbol on the end for example, "name$".
To declare a string variable, you must follow the "$" symbol with the maximum length of string you want the variable to handle, in brackets. So if you want to store names up to 15 characters long in the variable "NAME$", declare it like this: "LOCAL NAME$(15)". Strings cannot be longer than 255 characters.
You may want a group of variables, for example to store lists of values. Instead of having to declare separate variables "a", "b", "c", "d" and "e", you can declare array variables "a(1)" to "a(5)" in one go like this:
"LOCAL a%(5)" (array of integer variables) "LOCAL a(5)" (array of floating-point variables) "LOCAL a$(5,8)" (array of string variables) or "LOCAL a&(5)" (array of long integers)
The number in brackets is the number of elements in the array. So "LOCAL a%(5)" creates five integer variables: "a%(1)", "a%(2)", "a%(3)", "a%(4)" and "a%(5)".
With strings, the second number in the brackets specifies the maximum length of the strings. All the elements in the string array have the same capacity for example, "LOCAL ID$(5,10)" allocates memory space for five strings, each up to ten characters in length.
OPL does not support two-dimensional arrays.
All numeric variables have zero as their initial value. String variables have a string with no characters in it. Every element in an array variable is also initialised in the appropriate way.
To make it easier to write your programs, and understand them when you read through them at a later date, give your main variables names which describe the values they hold. For example, in a procedure which calculates fuel efficiency, you might use variables named "speed" and "distance".
All variable names:
The "$ & "and "%" symbols are included in the 8 characters allowed in variable names so "V2345678%" is too long to be a valid variable name, but "V234567%" is acceptable.
You can assign a value to a variable directly, like this:
This procedure adds two numbers together:
PROC add: LOCAL x%,y%,z% x%=569 y%=203 z%=x%+y% PRINT z% GET ENDP
"add:" is the procedure name.
The "LOCAL" statement defines three variables "x%", "y%" and "z%", all initially with the value 0. "PRINT" displays the value of "z%" on the screen. You can display the value of any variable like this.
"PROC" and "ENDP" define the beginning and end of the procedure as you saw in Chapter 1.
Assigning values to string variables String variables can be assigned text values like this:
The text to use must be enclosed in double quote characters.
Assigning values to an array variable If you declare a%(4), assign values to each of the elements in the array like this: "a%(1)=56, a%(2)=345" and so on. Similarly for the other variable types: "a(1)=.0346, a&(3)=355440, a$(10)="name"".
You can use these operators:
+ plus - minus or make negative / divide * multiply ** raise to a power % percentage
Operators have the same precedence as in the Calculator. For example, "3+51.3/8" is treated as "3+(51.3/8)", not "(3+51.3)/8". For more information on operators and precedence, see Appendix B.
There are two kinds of keyword commands and functions:
The number returned by GET will always be a small whole number, so you might store it away in an integer variable, like this:
There is more about the GET function later in this chapter.
You can assign a value to a variable with an expression that is, a combination of numbers, variables, and functions. For example:
"z=x+y/2" gives the "z" the value of "x" plus the value of " y/2 ".
"z=x*y+34.78" gives "z" the value of "x" times "y", plus "34.78".
"z=x+COS(y)" gives "z" the value of "x" plus the cosine of "y". COS is another OPL function. Unlike the GET function, COS requires a value or variable to work with. As you can see, you put this in brackets, after the function name. Values you give to functions in this way are called arguments to the function. There is more information about arguments in the next chapter.
All of the above are operations using the variables "x" and "y" assigning the result to "z" and not actually affecting the value of "x" or "y".
The ways you can change the values of variables fall into these groups:
"z%=z%+1" (make the value of "z%" one greater than its current value)
"x%=y+x%/4" (make the value of "x%" a quarter of its current value, plus the value of "y")
In an OPL program, numbers (and strings in quote marks) are sometimes called constants. In practice, you will use constants without thinking about them. For example:
x=0.32 x%=569 x&=32768 x$="string" x(1)=4.87OPL can also represent hexadecimal constants. This is explained under the HEX$ entry in the `Alphabetic listing' chapter.
Exponential notation may be useful for very large or very small numbers. Use "E" (capital or lower case) to mean "times ten to the power of" for example, 3.14E7 is 3.14*10^7 (31400000), while 1E-9 is 1*10^-9 (0.000000001).
When calculating an expression, OPL uses the simplest arithmetic possible for the numbers involved. If all of the numbers are integers, integer arithmetic is used; if one is outside integer range but within long integer range, then long integer arithmetic is used; if any of the numbers are not whole numbers, or are outside long integer range, floating-point arithmetic is used.
This has the benefit of maximising speed, but you must beware of calculations going out of the range of the type of arithmetic used. For example, in "X=200*300" both "200" and "300" are integers, so integer arithmetic is used for speed (even though "X" is a floating-point variable). However, the result, 60000, cannot be calculated because it is outside integer range (32767 to -32768), so an `Integer Overflow' error is produced.
You can get around this by using the INT function, which turns an integer into a long integer, without changing its value. If you rewrite the previous example as "X=INT(200)*300", OPL has to use long integer arithmetic, and can therefore give the correct result (60000). (If you understand hexadecimal numbers, you can instead write one of the numbers as a hexadecimal long integer eg "200" would become "&C8".)
Integer arithmetic uses whole numbers only. For example, if "y%" is 7 and "x%" is 4, "y%/x%" gives 1. However, you can use the INTF function to convert an integer or long integer into a floating-point number, forcing floating-point arithmetic to be used for example, "INTF(y%)/x%" gives 1.75. This rule applies to each part of an expression -e.g. "1.0+2/4" works out as "1.0+0" (=1.0), while "1+2.0/4" works out as "1+0.5" (=1.5).
If one of the integers in an all-integer calculation is a constant, you can instead write it as a floating-point number. 7/4 gives 1, but 7/4.0 gives 1.75.
If "a$" is ""down"" and "b$" is ""wind"", then the statement "c$=a$+b$" means "c$" becomes ""downwind"".
Alternatively, you could give "c$" the same value with the statement "c$="down"+"wind"".
When adding strings together, the result must not be longer than the maximum length you declared eg if you declared "LOCAL a$(5)" then "a$="first"+"second"" would cause an error to be displayed.
Most operators do not work on strings. To cut up strings, use string functions like MID$, LEFT$ and RIGHT$, explained in a later chapter. You need them to extract even a single character you cannot, for example, refer to the 4th character in "a$(7)" as "a$(4)".
PRINT is one of the most useful OPL commands. Use it to display any combination of text messages and the values of variables.
In general, each PRINT statement ends by moving to a new line. For example:
A%=127 :PRINT "A% is" PRINT a%"would display as
A% is 127
You can stop a PRINT statement from moving to a new line by ending it with a semicolon. For example:
A%=127 :PRINT "A% is"; PRINT a%would display as
A% is127If you end a PRINT statement with a comma, it stays on the same line but displays an extra space. For example:
A%=127 :PRINT "A% is", PRINT a%would display as
A% is 127
You can use commas or semicolons to separate things to be displayed on one line, instead of using one PRINT statement for each. They have the same effect as before: " A%=127 :PRINT "A% is",a%" would display as " A% is 127" while " user$="Fred"" " PRINT "Hello",user$;"!"" would display as " Hello Fred!"
Each string you use with PRINT must start and end with a quote character. Inside the string to display, you can represent the quote character itself by entering it twice. So "PRINT "Press "" key"" displays as "Press " key", while "PRINT """"" displays a single quote character.
If you want a program to be reusable, it often needs to be able to accept different sets of information each time you use it. You can do this with the INPUT command, which takes numbers and text typed in at the keyboard and stores them in variables.
For example, this simple procedure converts from Pounds Sterling to Deutschmarks. It asks you to type in two numbers the number of Pounds Sterling, and the current exchange rate. You can edit as you type the numbers the Delete key, for example, deletes characters, and Esc clears everything you've typed. Press Enter when you've finished each number. The values are assigned to the variables "pounds" and "rate", and the result of the conversion is then displayed:
PROC exch: LOCAL pounds,rate AT 1,4 PRINT "How many Pounds Sterling?", INPUT pounds :REM value from keyboard PRINT "Exchange rate (DM to œ1)?", INPUT rate :REM value from keyboard PRINT "=",pounds*rate,"Deutschmarks" GET ENDP
Here PRINT is used to show messages (often called prompts) before the two INPUT commands, to say what information needs to be typed in. In both cases the PRINT command ends in a comma, which displays a single space, and keeps the cursor position on the same line. Without the commas, the numbers you type to the INPUT commands would appear on the line below.
The value entered to an INPUT command must be of the appropriate kind for the variable which INPUT is setting. If you enter the wrong type (for example, if you enter the string "three" for the floating-point variable "rate"), INPUT will show a "?" prompt, and wait for you to enter another value.
When using INPUT with a numeric variable (integer, long integer or floating-point), you can enter any number within the range of that type of variable. Note that if you enter a non-whole number as the value for an integer variable, it will take only the whole number part (so eg if you enter 12.75 for an integer variable, it will be set to 12).
The REM command lets you add comments to a program to help explain how it works. Begin the comment with the word REM (short for `remark'). Everything after the REM command is ignored.
If you put a REM command on the end of a line, the colon you would normally put before it is optional. For example, you could use either of these:
CLS :REM Clears the screen
CLS REM Clears the screen
This positions the cursor or your message at the co-ordinates you specify. Use the command like this:
where "column%" and "row%" give the character position to use.
"AT 1,1" positions the cursor to the top left corner.
In addition to using INPUT to ask for values, your program can ask for single keypresses. Use one of these functions:
KEY$ and GET$ work in the same way as KEY and GET, except that they return the key pressed as a string, not as a character code:
PROC kchar: LOCAL k$(1) PRINT "Press a key, A-Z:" k$=GET$ PRINT "You pressed",k$ PAUSE 60 ENDP
Single keypresses are often useful for making decisions. A program might, for example, offer a set of choices which you choose from by typing the word's first letter, like this: "Add (A) Erase (E) or Copy (C) ?" Or it might ask for confirmation of a decision, by displaying a "YES or NO?" message and waiting until "Y" or "N" is pressed.
If you need to check for the Shift, Control, Psion keys and/or Caps Lock being used, see the description of the KMOD function, in the `Alphabetic listing' chapter.
Declare variables with one or more LOCAL statements in the line after "PROC:"
AT positions the cursor.
GET and KEY return the key pressed as a character code.
GET$ and KEY$ return the key pressed as a single-character string.
GET and GET$ wait until a key is pressed, KEY and KEY$ do not.