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
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
END
exits program, and clears all variables, which frees up memory.
STOP
exits 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 Debug
menu, and then Start
from the Run
menu. END
has the same effect as STOP
+ choosing Restart
from the Run
menu 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
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.
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.
The answer is
exit();
to exit the program.