Q(uick)BASIC Statement: RUN

Quick View

RUN

A control flow statement that restarts the program currently in memory, or executes a specified program

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • RUN [{linenumber | file$}]
Description/Parameter(s)
linenumber The line number in the current program where execution should begin. If no line number is specified, execution begins at the first executable line.
file$ The name of a Basic source file. QBasic assumes a .BAS extension.
RUN closes all files and clears program memory before loading a program. Use the CHAIN statement to run a program without closing open files.
Example
'Assumes the program TEST.BAS is in a \DOS directory. RUN "C:\DOS\TEST.BAS"

See also:

Syntax
  • RUN [{linenumber | filespec}]
Description/Parameter(s)
Argument Description
linenumber The numeric label of the line where execution begins. If no argument is given, execution begins at the first executable line.
filespec A string expression naming the program file to load and run. The current program is cleared from memory before the specified program is loaded.

The line where execution begins must be in the module-level code. Therefore, a RUN statement in a SUB or FUNCTION procedure must point to labels at module level. If no line label is given, execution always starts with the first executable line of the main module.

During compilation, if linenumber cannot be found in the module-level code, compilation halts and the error message Label not defined appears.

Program lines can have line numbers or alphanumeric labels, such as OpenWindow:. If an alphanumeric label is the target of a RUN statement, execution halts, and the error message "Type mismatch" appears. Note that the QuickBASIC syntax checker does not warn you if you give the RUN statement an alphanumeric label instead of a line number.

You do not need to specify the file name extension in filespec. The .BAS extension is assumed in the QuickBASIC environment. QuickBASIC assumes the .EXE extension for compiled, stand-alone programs. If the program you wish to run has a different extension, you must give the extension. If the program name has no extension, the file name given must end with a period. For example,

  • RUN "CATCHALL"

would execute CATCHALL.EXE from a BC-compiled program, and CATCHALL.BAS from within QuickBASIC.

Programs running within the QuickBASIC environment must call only QuickBASIC program files. The file is loaded and run as if it were a QuickBASIC program; if it is not in the QuickBASIC program format, execution halts. The error message that appears varies, depending on the file's contents. Likewise, programs compiled with BC must not invoke QuickBASIC source files, as these run only in the QuickBASIC environment.

An executable file need not have been written in QuickBASIC. Any executable file may be run.

When running a program under QuickBASIC, if an executable file matching the file name in commandline cannot be found, the error message "File not found" appears, and control returns to QuickBASIC. When running a program compiled by BC, the error message "File not found in module programname" appears, and control returns to DOS.

When the invoked program completes execution, control does not return to the invoking program. If the invoking program ran outside QuickBASIC, control returns to DOS. If the invoking program ran under QuickBASIC, control returns to QuickBASIC.

If you edit a QuickBASIC program containing a RUN statement, then run the program before saving the changes, QuickBASIC asks if you wish to save the new version of the program before RUN clears it from memory.

RUN closes all files and clears program memory before loading the designated program. The BC compiler does not support the R option from BASICA. (The R option keeps all open data files open.) If you want to run a different program, but leave open files open, use the CHAIN statement.

Example

This example shows how RUN linenumber resets all numeric variables to 0. As the line number following RUN increases in lines 60, 70, 80, and 90, the variables in the earlier statements lose their assigned values:

10 A = 9 20 B = 7 30 C = 5 40 D = 4 50 PRINT A, B, C, D 60 IF A = 0 THEN 70 ELSE RUN 20 70 IF B = 0 THEN 80 ELSE RUN 30 80 IF C = 0 THEN 90 ELSE RUN 40 90 IF D = 0 THEN END ELSE RUN 50

Sample Output:

9 7 5 4 0 7 5 4 0 0 5 4 0 0 0 4 0 0 0 0

See also:

Syntax
  • RUN [{linenumber | filespec$}]
Description/Parameter(s)
linenumber The numeric label of the line where execution begins. If no argument is given, execution begins at the first executable line.
filespec$ A string expression that names the program file to load and run. The current program is cleared from memory before the specified program is loaded.
  • You do not need to specify the filename extension in filespec$. The .BAS extension is assumed in the QBX environment. BASIC assumes the .EXE extension for compiled, stand-alone programs. If the program you wish to run has a different extension, you must give the extension. If the program name has no extension, the filename given must end with a period. For example,

  • RUN "CATCHALL"

  • would execute CATCHALL.EXE from a BC-compiled program, and CATCHALL.BAS from within the QBX environment.

Usage Notes

  • The line where execution begins must be in the module-level code. Therefore, a RUN statement in a SUB or FUNCTION procedure must point to labels at module level. If no line label is given, execution always starts with the first executable line of the main module.
  • Program lines can have line numbers or alphanumeric labels, such as OpenWindow:. However, the RUN statement does not accept an alphanumeric label. If an alphanumeric label is the target of a RUN statement, the compiler generates the error message, "String expression required."
  • Programs running within the QBX environment must call only BASIC program files. The file is loaded and run as if it were a BASIC program; if it is not in the BASIC program format, execution halts. The error message that is displayed varies, depending on the file's contents. Likewise, programs compiled with BC must not invoke BASIC source files, as these run only in the QBX environment.
  • An executable file need not have been written in BASIC. Any executable file can be run.
  • When running a program under QBX, if an executable file matching the filename in commandline cannot be found, BASIC generates the error message, "File not found" and control returns to QBX. When running a program compiled by BC, the error message, "File not found in module programname " is displayed, and control returns to the operating system."
  • When the invoked program completes execution, control does not return to the invoking program. If the invoking program ran outside QBX, control returns to the operating system. If the invoking program ran under QBX, control returns to QBX.
  • RUN closes all files and clears program memory before loading the designated program. The BC compiler does not support the R option from BASICA. (The R option keeps all open data files open.) If you want to run a different program, but leave open files open, use the CHAIN statement.
Example

This example uses the RUN statement to restart the program. The example shows that RUN resets all numeric variables to 0. As the line number following RUN increases in lines 60, 70, 80, and 90, the variables in the earlier statements lose their assigned values.

10 A = 9 20 B = 7 30 C = 5 40 D = 4 50 PRINT A, B, C, D 60 IF A = 0 THEN 70 ELSE RUN 20 70 IF B = 0 THEN 80 ELSE RUN 30 80 IF C = 0 THEN 90 ELSE RUN 40 90 IF D = 0 THEN END ELSE RUN 50

Sample Output:

9 7 5 4 0 7 5 4 0 0 5 4 0 0 0 4 0 0 0 0

See also: