Q(uick)BASIC Statement: END

Quick View

END

A BASIC declaration that ends a BASIC program, procedure, or block

Worth knowing

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

Syntax
  • END [{DEF | FUNCTION | IF | SELECT | SUB | TYPE}]
Description/Parameter(s)
DEF Ends a multiline DEF FN function definition.
FUNCTION Ends a FUNCTION procedure definition.
IF Ends a block IF...THEN...ELSE statement.
SELECT Ends a SELECT CASE block.
SUB Ends a SUB procedure.
TYPE Ends a user-defined data type definition.

If no argument is supplied, END ends the program and closes all files.

Example
PRINT "Game over." END
Syntax
  • END [{DEF | FUNCTION | IF | SELECT | SUB | TYPE}]
Description/Parameter(s)
Statement Description
END DEF Ends a multiline DEF FN function definition. You must use END DEF with a multiline DEF FN.
END FUNCTION Ends a FUNCTION procedure definition. You must use END FUNCTION with FUNCTION.
END IF Ends a block IF...THEN...ELSE statement. You must use END IF with block IF...THEN...ELSE.
END SELECT Ends a SELECT CASE block. You must use END SELECT with a SELECT CASE statement.
END SUB Ends a BASIC subprogram. You must use END SUB with SUB.
END TYPE Ends a user-defined type definition. You must use END TYPE with TYPE.

By itself, the END statement stops program execution and closes all files. In a stand-alone program, END returns control to the operating system. When running inside the QuickBASIC environment, END returns you to that environment.

The compiler always assumes an END statement at the conclusion of any program, so omitting an END statement at the end of a program still produces proper program termination.

You may place END statements anywhere in the program to end program execution.

Examples

These examples show how to use END IF and END SELECT forms of the END statement to terminate IF...THEN...ELSE and SELECT CASE blocks.
Here is an example of using END IF with a block IF...THEN...ELSE for a multiple-choice decision:

INPUT X IF X = 1 THEN PRINT "one" ELSEIF X = 2 THEN PRINT "two" ELSEIF X = 3 THEN PRINT "three" ELSE PRINT "must be integer from 1-3" END IF

This example uses the SELECT CASE structure to do the same thing:

INPUT X SELECT CASE X CASE 1 PRINT "one" CASE 2 PRINT "two" CASE 3 PRINT "three" CASE ELSE PRINT "must be integer from 1-3" END SELECT
Syntax
  • END [{DEF | FUNCTION | IF | SELECT | SUB | TYPE}]
  • END (n%)
Description/Parameter(s)
  • The value n% can be used by DOS or OS/2 batch files or by non-BASIC programs. Untrapped errors and fatal errors set the value of n% to -1.

Usage Notes

  • By itself, END stops program execution and closes all files. In a stand-alone program, END returns control to the operating system; in the QBX environment, END returns to the environment.
  • The following table shows where END is required:
Location Required?
End of a program No
End of multiline DEF FNfunction definition Yes
End of a FUNCTION procedure definition Yes
End of a block IF...THEN...ELSE statement Yes
End of a SELECT CASE block Yes
End of a BASIC SUB procedure Yes
End of a user-defined type definition (TYPE) Yes
  • You can place END statements anywhere in the program to terminate execution.
Example

The program uses the ON ERROR statement to enable error trapping. The program attempts to write a large file to the disk. If an error occurs, control is passed to a handling routine, and the ERR function is used to determine an appropriate message to display. The ERROR, ERR, and RESUME statements are used to exit the handling routines, and the END, STOP, and SYSTEM statements demonstrate ways to exit a program.

DECLARE SUB ErrorMessage (Message$) DECLARE SUB WriteBigFile (Filenum%) ON ERROR GOTO ErrHandler CLS PRINT "This program will attempt to write a large file to a disk drive" PRINT "you select. The file will be erased when the program ends." PRINT DO INPUT "Which drive"; DR$ DR$ = UCASE$(DR$) LOOP UNTIL LEN(DR$) >= 1 AND LEN(DR$) <= 2 AND DR$ >= "A" AND DR$ <= "Z" IF LEN(DR$) > 1 THEN IF RIGHT$(DR$, 1) <> ":" THEN DR$ = LEFT$(DR$, 1) + ":" END IF ELSE DR$ = DR$ + ":" END IF 'Put together a complete file specification. FileSpec$ = DR$ + "BIGFILE.XXX" 'Get the next available file number. Filenum% = FREEFILE 'Try to open the file. OPEN FileSpec$ FOR OUTPUT AS Filenum% WriteBigFile Filenum% CLOSE Filenum% CLS PRINT "Everything was OK. No errors occurred." PRINT "Deleting the file that was created." KILL FileSpec$ 'Same as END, returns to operating system. SYSTEM ErrHandler: SELECT CASE ERR CASE 52 'Bad file name or number. END CASE 53 'File not found. RESUME NEXT CASE 57 'Device I/O error. ErrorMessage "You should probably format the diskette." END CASE 64 'Bad File Name. ErrorMessage "The drive name you specified was not correct." END CASE 68 'Device unavailable. ErrorMessage "The drive you named is unavailable." END CASE 71 'Drive not ready. ErrorMessage "The drive was not ready. Check the drive!" END CASE ELSE ErrorMessage "An unexpected FATAL error has occurred." STOP END SELECT SUB ErrorMessage (Message$) ON LOCAL ERROR GOTO MessageError CLS PRINT Message$ PRINT "Cannot continue." PRINT PRINT "Press any key to exit." DO LOOP WHILE INKEY$ = "" EXIT SUB MessageError: RESUME NEXT END SUB SUB WriteBigFile (Filenum%) ON LOCAL ERROR GOTO LocalHandler TEXT$ = STRING$(1024, "A") FOR I% = 1 TO 400 PRINT #Filenum%, TEXT$ NEXT I% EXIT SUB LocalHandler: SELECT CASE ERR CASE 61 'Disk full. ErrorMessage ("There is no room remaining on the disk.") KILL "BIGFILE.XXX" END CASE ELSE ERROR ERR END SELECT END SUB