2

I am currently writing a QBASIC program that runs an indefinite loop (while loop). However, if a certain condition is met, I want to exit the program. What command do I use, and also what is the syntax.

Thanks

csrockstar
  • 55
  • 1
  • 9

4 Answers4

5

ENDexits program, and clears all variables, which frees up memory. STOPexits program, but retains the value of all variables, which makes it possible (in certain versions of QB) to continue execution at another point, by choosing Set next statement from the Debugmenu, and then Startfrom the Runmenu. END has the same effect as STOP + choosing Restart from the Runmenu once the program has terminated.

If you have a loop, and want to exit the program from inside it, you may use either

DO IF condition THEN EXIT DO LOOP END

or

DO IF condition THEN END LOOP

Alpha_Pi
  • 383
  • 2
  • 13
4

You're looking for the END or SYSTEM statement. For example:

PRINT "Hello World!"
END
PRINT "This won't be printed."

If you're using regular old QBASIC/QuickBASIC, then you can ignore all of the QB64 details on the linked pages and just use either SYSTEM or END. Both will do the same thing for the most part.1

If you're using FreeBASIC, it's recommended to use END instead of SYSTEM since some things won't get cleaned up properly when you use SYSTEM. See SYSTEM for more information pertaining to FreeBASIC if that's what you're using.


1 The END statement when running the program using QB.EXE /RUN PROGRAM.BAS will print "Press any key to continue" before exiting to the QB/QBASIC environment. The SYSTEM statement when run the same way will simply return you to the DOS shell without any need for a key press. Also, typing SYSTEM in the "Immediate Window" of the QB/QBASIC environment will exit the environment and return to the DOS shell. Otherwise the two statements behave exactly the same in QB/QBASIC, whether for standalone (compiled) programs or .BAS modules.

1

You can keep any condition according to the need of your program. For eg:

CLS
LET a = 5
WHILE a > 0
      PRINT a;
      a = a - 1
WEND
END

Here, in the program while wends executes itself until a = 0. This will not run an infinite loop.

therealyubraj
  • 112
  • 12
-3

The answer is

exit();

to exit the program.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
xyztg
  • 7
  • -1 This is a question about how to end the program using QBASIC, not C or C++. Actually, this isn't a correct answer for any of those languages since you pass an integer value to the function in both C and C++. –  Feb 13 '16 at 00:41