# Directives, Statements and Programs

> Source: https://manual.pvxplus.com/pxplus-user-guide/language-elements/directives-statements-and-programs/overview

All processing in PxPlus is controlled by ***Directives*** - keywords that "direct" the system to perform certain tasks. Directives can be entered in Command mode to be executed immediately. They also represent the core ingredient in the ***statements*** that constitute a PxPlus ***program***.

***Example:***

![](https://manual.pvxplus.com/migration-assets/dddac7ee5d407861328db32ae5b5bcbf238ea6842ec76d25d9970bad735a9c01.jpg)

In the example above, the **[PRINT](https://manual.pvxplus.com/directives/print)** directive is executed as soon as it is entered on the Command line. However, the ***line number*** inserted before the **[LET](https://manual.pvxplus.com/directives/let)** directive prevents that input from being executed - when ***numbered statements*** are entered:

- The input is not executed but added to a program in memory.
- The prompt changes from **->** to **-:** to indicate that a program is being ***edited***.
- The series of numbered statements currently in memory will only be executed when a **[RUN](https://manual.pvxplus.com/directives/run)** or a **[CALL](https://manual.pvxplus.com/directives/call)** directive is issued.

Each statement can be made up of one or more directives. A PxPlus program consists of a series of statements that are organized in a logical manner and stored together in memory. When a program is executed, all the statements (and the directives inside each statement) are evaluated and processed in the order in which they are read by the system. Once the directives in one statement have been executed, PxPlus proceeds to the next statement (next line of code) in the sequence, and so on.

Note:  
Some statements can alter the normal (top to bottom) flow of execution by transferring control to other points in the program. See [Programming Constructs](https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/introduction).

In theory, each program statement begins and terminates on one line - a carriage return marks the end of a line of code, and PxPlus executes it as though it were entered directly on the Command line. However, there are exceptions to this rule; e.g. if the **[LIST EDIT](https://manual.pvxplus.com/directives/list)** directive is used to *format* program code for readability, lengthy statements may be listed over several lines - the line number representing the entire statement appears on the first line only.

## Statement Syntax

A valid program statement requires at least one directive. Depending on the directives used, various arguments and syntax elements may be needed to complete the statement. The example below illustrates the format of a typical program statement:

***Example:***

```
0010 Total: print a,b,c+d ! Totals
```

***Where:***

|  |  |
| --- | --- |
| 0010 | ***Line Number*** *(Optional)*  <br>  <br>Any integer between 1 and 64999. Each line number must be unique - the best practice is to assign line numbers in increments of 5 or 10 to allow for easy insertion of additional statements. When a line number precedes a statement, the statement will be included in the construction of a program.<br>See **[Line Numbers](https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/flow-control/statement-references#linenumbers)**. |
| Total: | ***Line Label*** *(Optional)*  <br>  <br>Descriptive name up to 127 characters long, followed by a **:** (*colon*). Alphanumeric line labels can be used in place of line numbers to identify statements within the program. |
| print | ***Directive***  <br>  <br>Keyword identifying the task to be performed. |
| a,b,c+d | ***Arguments*** *(Optional)*  <br>  <br>Data to be processed or system keywords that further define the directive. A space is required to separate the directive from its arguments.<br>See **[Arguments](https://manual.pvxplus.com/pxplus-user-guide/language-elements/directives-statements-and-programs/overview#arguments)**. |
| ! Totals | ***Comment*** *(Optional)*  <br>  <br>Descriptive text beginning with an **!** (*exclamation point*) or the keyword **REM**. Text following an exclamation point is ignored by the system; i.e. the exclamation point terminates a statement and passes execution to the next line of code. |

Note:  
Comments are very important for the readability and maintainability of a program. However, this text is included in the final compiled program; therefore, be aware that excessive comments may increase memory requirements and degrade overall system performance.

***Arguments***

While some directives are executed without arguments, most accept/require data or keywords to perform their tasks. Argument components may be optional or mandatory - this depends on the directive and the task involved.

Most arguments supply some form of data for processing - a text or numeric value delivered in literal or variable form. See **[Data Types, Literals and Variables](https://manual.pvxplus.com/pxplus-user-guide/language-elements/data-types-literals-and-variables/overview)**.

Other types of arguments can redefine or augment the functionality of the directive itself - system parameters, mnemonics, options, and built-in keywords. See **[Primary Syntax Elements](https://manual.pvxplus.com/pxplus-user-guide/language-elements/primary-syntax-elements/overview)**.

***Compound Statements***

If more than one directive appears in a statement, it is considered a ***compound*** statement. *Semi-colons* (**;**) are required to separate the different directives (and their arguments) until the end of the line:

```
0020 let x=a+b; print "Answer=",x; goto 1000
```

Each of the directives in a compound statement (between the semi-colons) would function the same if they were entered in sequence on separate lines.

A transfer of control into a compound statement starts processing from the ***first*** directive only. In the above example (statement 0020), it would be impossible to start processing at the **[PRINT](https://manual.pvxplus.com/directives/print)** or **[GOTO](https://manual.pvxplus.com/directives/goto)** directive. However, it is possible to transfer control out of (and then back into) a compound statement if the directive is designed to return back to the current location (see **[GOSUB..RETURN](https://manual.pvxplus.com/directives/gosub)** and **[FOR..NEXT](https://manual.pvxplus.com/directives/for)**):

```
0030 x=10;print x; gosub xyz; goto 1000
```

Directives that transfer control without returning (e.g. **[GOTO](https://manual.pvxplus.com/directives/goto)**, **[EXITTO](https://manual.pvxplus.com/directives/exitto)**, **[RETURN](https://manual.pvxplus.com/directives/return)**) must appear at the end of a compound statement. The following is not valid because **RETURN** transfers control and the last statement is unreachable code:

```
print "hello";
return ;
if name$<>"" \
     then print "-",name$
```

Some directives cannot be included in a compound statement. Refer to the individual directives for any restrictions.

## Saving, Loading and Executing a Program

A PxPlus program is a collection of statements that are stored in memory (or saved to a file) for consecutive execution. In Command mode, the ***current program*** represents all the statements in memory that are immediately available for editing and/or execution. In Execution mode, the current program is also referred to as the ***main-line*** program (also known as *execution level 1*).

The current program can be **RUN** without being saved to a file. Once saved, a program can also be **RUN** directly from a file. By default, program files are saved in a semi-processed state that allows them to be executed as though they are already loaded in memory. These pre-compiled tokenized program files are not readable in a standard text editor and must be loaded into a PxPlus session for editing. For information on working with text-based program files, see **[Text (ASCII) Format](https://manual.pvxplus.com/pxplus-user-guide/language-elements/directives-statements-and-programs/overview#ascii)**.

Whether you create a new program or **LOAD** an existing program from a file, all changes you make to numbered statements at the Command line will apply to the program currently loaded in memory.

For the different methods for creating and saving PxPlus programs, see **[Writing and Modifying Program Code](https://manual.pvxplus.com/pxplus-user-guide/development-tools/writing-and-modifying-program-code/overview)**.

***Saving a Program to a File***

During a PxPlus session (in Command mode), the current program will cease to exist if a **[START](https://manual.pvxplus.com/directives/start)** or **[DELETE](https://manual.pvxplus.com/directives/delete)** is issued, or another program is loaded. All changes to the current program will be lost.

To retain your changes for later use, use the **[SAVE](https://manual.pvxplus.com/directives/save)** directive to copy the current program to a file on disk:

```
save "Myprogram"
```

If the file name specified by *"Myprogram"* does not exist, a new program file is created, and the program is written to it. If a program exists with the same name, it will be overwritten by the current program. Programs can also be saved in program libraries.

See **[\[LIB\] Program Library](https://manual.pvxplus.com/command-tags/lib)**.

***Retrieving a Program File***

Programs that are saved to disk may be recalled using the **[LOAD](https://manual.pvxplus.com/directives/load)** directive. This clears the current program (if any) and replaces it with the program specified:

```
load "Myprogram"
```

***Executing a Program***

The **[RUN](https://manual.pvxplus.com/directives/run)** directive can be used to execute the current program. If the **RUN** command includes a program name, the system will automatically load the specified program from disk before executing it:

```
run "Myprogram"
```

***Text (ASCII) Format***

It is possible to **[RUN](https://manual.pvxplus.com/directives/run)**, **[CALL](https://manual.pvxplus.com/directives/call)**, **[PERFORM](https://manual.pvxplus.com/directives/perform)**, **[LOAD](https://manual.pvxplus.com/directives/load)** and **[SAVE](https://manual.pvxplus.com/directives/save)** PxPlus programs in text (ASCII) file format. If the target file for a **SAVE** is not a program file, PxPlus will automatically write the program out in ASCII format:

```
delete
for I=1 to 20
     print I
next
serial "PROG999"
save "PROG999"
```

This would output the program to the text file PROG999. If desired, you can insert the keyword **EDIT** following the **[SAVE](https://manual.pvxplus.com/directives/save)** directive, and the system will save the program in formatted form. Text-based PxPlus programs do have some advantages:

- Use of more advanced editing/formatting functionality that is available in other types of editors (spell checking, multiple undo, etc.).
- Version control and configuration management systems.
- Accessing multiple files via bulk editing tools.

However, when running a text-based program from a PxPlus session, each statement must be evaluated for syntax and converted to tokenized code prior to execution. These extra steps may have an impact on system performance. One way to avoid a performance hit is to **[LOAD](https://manual.pvxplus.com/directives/load)** a text-based program prior to executing it. At this point, it can be **RUN** as the current program in memory.

## See Also

**[Directives](https://manual.pvxplus.com/directives)**
