Internal Program Control

Statements in an mvBASIC program are executed in the order in which they appear in the source code. Program control statements are those which can be used to alter that sequence. This section discusses the internal program control constructs that do not involve other programs, external subroutines, or TCL commands. More advanced program control statements are discussed later in this section.

The IF Conditional

The IF construct is used to execute a statement (or series of statements) if a condition has a logical value of true, and (optionally) a different set of statements if the condition has a logical value of false.

The THEN and ELSE Clauses

Either a THEN or ELSE clause (or both) must be supplied with IF. The syntax of the THEN and ELSE clauses is important to understand, because they are used not only in IF statements, but also in numerous file I/O, tape I/O, floppy disk I/O, and communication statements.

THEN and ELSE clauses can be written on the same statement line in the following manner:

IF NET >= 0 THEN PRINT "PROFIT IS ": ELSE PRINT "LOSS IS ": PRINT ABS(NET)

If there are multiple conditional statements for either the THEN or ELSE clause, they can be separated by semicolons (;). For example:

IF NET >= 0 THEN FLAG = 1; PRINT "PROFIT IS ": ELSE FLAG = 0; PRINT "LOSS  IS ": PRINT ABS(NET)

However, the IF statement becomes difficult to read if the THEN and ELSE clauses are written on a single line. It is preferable to write it on several lines, even if the conditional statement is very short.

When splitting an IF statement onto several lines, the THEN or ELSE keyword must end a program line, with the conditional statements beginning on the next. At the end of the conditional statements, an END statement must be used to group them together.

Thus, if the condition tested by IF is true, all statements between the THEN clause and the corresponding END statement are executed; otherwise, all statements between the ELSE clause and the corresponding END statement are executed.

So the above lines of a program printing out profit or loss on a transaction might read:

IF NET >= 0 THEN

FLAG = 1

PRINT "PROFIT IS " :

END ELSE

FLAG = 0

PRINT "LOSS IS " :

END

PRINT ABS(NET)

NULL Statements

NULL statements are often included in THEN or ELSE clauses as placeholders. A NULL statement performs no action; however, a programmer can use NULL statements to make the logic of a conditional somewhat clearer: for example, if a programmer wanted to test if a value was numeric, he might write:

IF NUM(PRICE) THEN

NULL

END ELSE

PRINT "ERROR: NON-NUMERIC PRICE. STOP"

STOP

END

There are ways of doing this without using a NULL statement (by using the NOT function in the condition, or by omitting the THEN clause entirely). However, a programmer might prefer to use NULL statements to make conditionals easier to read.

CASE Constructs

The CASE construct acts to perform multiple IF conditionals. It tests several conditions until one returns a value of true. It then executes the associated set of statements.

A CASE construct must begin with a BEGIN CASE statement and end with an END CASE statement. In between, each CASE statement tests a single condition, and, if true, the statements between the current CASE and the next CASE are executed. The program then jumps to the position after the END CASE statement, ignoring all remaining CASE statements in that group.

For example, the above profit-or-loss example might read:

BEGIN CASE

CASE NET > 0

PRINT "PROFIT IS " : NET

CASE NET < 0

PRINT "LOSS IS " : ABS(NET)

CASE NET = 0

PRINT "NO PROFIT OR LOSS ON THIS TRANSACTION."

END CASE

Loops (LOOP, FOR)

Program loops are constructs that repeat the same sequence of statements while a condition holds true or until a condition is met.

The LOOP Construct

The LOOP statement is the general-purpose looping construct in mvBASIC. It has (optionally) two sets of statements, the first of which is executed before testing the condition clause, and the second of which is executed only if the condition is verified. The condition is written as either a WHILE or an UNTIL clause. If the WHILE clause is used, the loop will continue if the condition is still true; if the UNTIL clause is used, the loop will continue if the condition is still false.

The following example will continue to prompt for a number until a numeric value is entered:

LOOP

PRINT "ENTER A NUMBER":

INPUT NUMBER

UNTIL NUM(NUMBER) DO

PRINT "NUMERIC INPUT EXPECTED!"

REPEAT

The UNTIL clause can be replaced with a WHILE by negating the condition:

WHILE NOT(NUM(NUMBER)) DO

FOR Loops

In its simplest form a FOR loop performs a set of statements while incrementing a number by 1. When the number reaches or surpasses a specified maximum, the FOR loop exits. For example, a program printing out the first ten perfect squares might read:

FOR I = 1 TO 10

   PRINT I * I

NEXT I

In mvBASIC, the FOR loop has been enhanced in two ways: an increment other than 1 can be specified with the STEP clause, and the WHILE and UNTIL clauses of the LOOP statement have been incorporated into it.

Stopping a Program (STOP, ABORT, END)

Two statements will cause an immediate stop to program execution: the STOP statement and the ABORT statement. The difference between them is that if the current program is called by a Proc or another program, the STOP statement will return to the calling Proc or program, but an ABORT statement will return directly to TCL.

In general, STOP statements are used for a normal or nonfatal termination of a program, and an ABORT statement is used for abnormal termination.

STOP and ABORT are often used in ELSE clauses to file I/O, tape I/O, floppy disk I/O, or communication statements, when the program becomes pointless if the statement fails. STOP statements are also used between the main part of a program and its internal subroutines. When a program is written with internal subroutines at the end, a STOP statement is necessary to ensure that the subroutines are not directly executed at the end of the program.

The END Statement

Beyond its function for delimiting THEN or ELSE statements, the END statement is also used to designate the end of compilation. When the compiler reaches an END statement that does not correspond to a THEN, ELSE, or LOCKED clause, all compilation stops. Any statements or subroutines that come after the END statement in the source code will be ignored.

Internal Subroutines (GOSUB, RETURN)

An internal subroutine is a discrete sequence of statements starting with a statement label and ending with a RETURN statement. In the source code, subroutines are placed after the main part of the program, and precautions are generally taken to ensure that they are never executed directly. Internal subroutines are executed by GOSUB statements, which point to the statement label.

Example

A subroutine labelled REPORT prints a report of the session’s transactions. The REPORT subroutine might call the following code:

PRINT "PRINTING A REPORT ..."

GOSUB REPORT

DISPLAY "REPORT PRINTED."

The REPORT subroutine might read:

REPORT:

PRINTER ON

PRINT "NUMBER OF TRANSACTIONS" ,

NO.OF.TRANS

.

.

.

PRINTER CLOSE

PRINTER OFF

RETURN

When the GOSUB is executed, program control will transfer to the statement label REPORT and will continue until the RETURN statement is encountered. Program control will then continue with the statement following the GOSUB, and the message REPORT PRINTED will be displayed to the screen.

The GOTO Statement

The GOTO statement is often grouped together with GOSUB, because they share the same syntax and perform similar functions. However, the GOTO statement serves only to transfer program execution to the statement label and will never return unless another GOTO is used.

See Also

Overview of mvBASIC Statements and Functions

Assignment Statements

Intrinsic Functions

External Program Control

Sending Output to the Screen and Printer

Terminal Input

Dynamic Array Processing

Generalized String Processing

Dimensioned Arrays

Reading and Updating File Items

Reading and Writing Tapes or Floppy Disks

Communications

Execution Locks

Compiler Directives

Miscellaneous Statements and Functions

The Error Message Processor