Q(uick)BASIC Statement: EXIT

Quick View

EXIT

A control flow statement that exits a DEF FNfunction, DO...LOOP, FOR...NEXT loop, FUNCTION, or SUB

Worth knowing

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

Syntax
  • EXIT {DEF | DO | FOR | FUNCTION | SUB}
Description/Parameter(s)
DEF Exits a DEF FN function.
DO Exits a DO loop.
FOR Exits a FOR loop.
FUNCTION Exits a FUNCTION procedure.
SUB Exits a SUB procedure.
Example
i% = 0 DO i% = i% + 1 IF i% = 500 THEN EXIT DO LOOP PRINT "EXIT at"; i%
Syntax
  • EXIT {DEF | DO | FOR | FUNCTION | SUB}
Description/Parameter(s)
Statement Description
EXIT DEF Causes an immediate exit from the executing DEF FN function. Program execution continues where the DEF FN function was invoked.
EXIT DO Provides an alternative exit from a DO...LOOP. Can be used only inside a DO...LOOP statement; EXIT DO transfers control to the statement following the LOOP statement. When used within nested DO...LOOP statements, transfers out of the immediately enclosing loop.
EXIT FOR Provides another way to exit a FOR...NEXT loop. May appear only in a FOR...NEXT loop; transfers control to the statement following the NEXT statement. When used within nested FOR...NEXT loops, transfers out of the immediately enclosing loop.
EXIT FUNCTION Causes an immediate exit from a FUNCTION procedure. Program execution continues where the FUNCTION was invoked. Can only be used in a FUNCTION procedure.
EXIT SUB Immediately exits a SUB procedure. Program execution continues with the statement after the CALL statement. Can only be used in a SUB procedure.

None of the EXIT statements define the end of the structure in which they are used. EXIT statements only provide an alternative exit from the structure.

Example

This subprogram is an extended RTRIM$ function that removes trailing blanks, tabs, carriage returns, and line feeds from a string. The subprogram begins looking at the end of the string and uses the EXIT FOR statement to jump out of the loop when the first printing character is found.
See the STATIC statement examples for an example of using the EXIT SUB statement.
Note: Do not try to run this subprogram without a main program.

' Rtrim removes trailing blanks, tabs, carriage returns, ' and line feeds from a string. SUB Rtrim(S$) STATIC J=0 ' Begin at the end of the string and find the first ' character that isn't a blank, tab, carriage return, or ' line feed. FOR I = LEN(S$) TO 1 STEP -1 C$ = MID$(S$,I,1) IF C$ <> " " AND C$ <> CHR$(9) AND C$ <> CHR$(10) AND C$ <> CHR$(13) THEN J=I EXIT FOR END IF NEXT I ' Remove the unwanted trailing characters. S$=LEFT$(S$,J) END SUB
Syntax
  • EXIT {DEF | DO | FOR | FUNCTION | SUB}
Description/Parameter(s)
  • EXIT DEF causes an immediate exit from the executing DEF FNfunction. Program execution continues where the DEF FNfunction was invoked.
  • EXIT DO provides an alternative exit from a DO...LOOP. Can be used only inside a DO...LOOP statement; EXIT DO transfers control to the statement following the LOOP statement. When used within nested DO...LOOP statements, transfers out of the immediately enclosing loop.
  • EXIT FOR provides another way to exit a FOR...NEXT loop. can appear only in a FOR...NEXT loop; transfers control to the statement following the NEXT statement. When used within nested FOR...NEXT loops, transfers out of the immediately enclosing loop.
  • EXIT FUNCTION causes an immediate exit from a FUNCTION procedure. Program execution continues where the FUNCTION was invoked. Can be used only in a FUNCTION procedure.
  • EXIT SUB immediately exits a SUB procedure. Program execution continues with the statement after the CALL statement. Can be used only in a SUB procedure.

Usage Notes

  • None of the EXIT statements defines the end of the structure in which it is used. EXIT statements provide only an alternative exit from the structure.
Example

This example uses EXIT statements to exit a variety of control-flow structures. A loop is continuously executed until you press any key. Once a key is pressed, the next EXIT statement that executes will cause the program to end.

DECLARE SUB ExitDemo () CLS DO PRINT : PRINT "Entering/Reentering ExitDemo" ExitDemo SLEEP 1 LOOP WHILE INKEY$ = "" PRINT "Exiting 'EXIT' example program" SUB ExitDemo DO FOR I% = 1 TO 1000 Num% = INT(RND * 100) SELECT CASE Num% CASE 7 PRINT "Exiting For...Next Loop in ExitDemo SUB" EXIT FOR CASE 29 PRINT "Exiting Do...Loop in ExitDemo SUB" EXIT DO CASE 54 PRINT "Exiting ExitDemo SUB" EXIT SUB CASE ELSE END SELECT NEXT I% LOOP END SUB