# Commands

## Contents

- Overview
- Displaying variables

- Where the cursor goes after a PRINT
- Displaying a list of things
- Displaying the quote character

- Reading values from the keyboard

- Inputting a line
- Single keypresses
- Example using GET$
- Modifier keys

## Overview

This section describes what an OPL statement is and gives some useful techniques for reading values from the keyboard and printing them to the screen.

A statement is a line of a program that instructs the OPL interpreter to perform some action, for example printing a line of text to the screen or assigning to a variable.

Most statements other than assignment use keywords, There are two kinds of keyword — commands and function calls:

A command is just a straightforward instruction to OPL to do some particular thing. `PRINT` and `PAUSE`, for example, are commands.

A function is just like a command but it also returns a value which you can then use.

Assignment and calling functions are covered in the Values and expressions section.

## Displaying variables

Section Contents

- Where the cursor goes after a PRINT
- Displaying a list of things
- Displaying the quote character

`PRINT` is one of the most useful OPL commands. Use it to display any combination of text messages and the values of variables on the screen.

### Where the cursor goes after a PRINT

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% is127

If 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

### Displaying a list of things

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!

### Displaying the quote character

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.

## Reading values from the keyboard

Section Contents

- Inputting a line

- Comments
- AT command

- Single keypresses
- Example using GET$
- Modifier keys

### Inputting a line

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 e.g. if you enter 12.75 for an integer variable, it will be set to 12).

#### Comments

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

or

        CLS REM Clears the screen

#### AT command

This positions the cursor or your message at the co-ordinates you specify. Use the command like this:

        AT column%,row%

where `column%` and `row%` give the character position to use.

`AT 1,1` positions the cursor to the top left corner.

### Single keypresses

In addition to using `INPUT` to ask for values, your program can ask for single keypresses. Use one of these functions:

- `GET` waits for a keypress and returns the key pressed.
- `KEY` returns a key if any was pressed, but doesn’t wait for one.

Every separate letter, number or symbol has a number which represents it, called a character code. The full list of character codes — the character set — for may be found in Character codes. `GET` and `KEY` return the character code of the key pressed for example, if `A` were pressed, these functions would return the value 65. `KEY` returns 0 if no key was pressed.

`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:

- `GET$` waits for a keypress and returns the key pressed, as a string.
- `KEY$` returns a key if any was pressed, but doesn’t wait for one. `KEY$` returns a null string if no key was pressed.

Unlike `INPUT`, these functions do not display the key pressed on the screen, and do not wait for you to press Enter.

### Example using GET$

        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.

See Control structures for details of how to identify which key is pressed.

### Modifier keys

If you need to check for the Shift, Control, Fn keys and/or Caps Lock being used, see the description of the `KMOD` function, in the Keyword Reference section.