Getting Started
Introduction
Sample Programs
QBasic IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Sample Programs

Sample Programs
Getting right to the code, this page gives two different program examples. The first is a very simple program, showing how minimal a QBasic program can be. The second is also small but shows some of the features useful for creating utility programs.

Simple Program
The tutorial section of this site contains information about writing QBasic programs, but here's a quick look at a traditional "Hello World" program.

   msg$="Hello World"
   print msg$

And here's a line-by-line analysis of what the code does.

  • msg$="Hello World"
    Assigns a value to a string variable (the $ defines msg as a string).

  • print x$
    Prints the string variable to the screen.

The QBasic interpreter filename is "qbasic.exe" so to run the program above, simply type into the QBasic editor and press F5.

Or, you can save the code into a text file, such as "myscript.bas". Then, within a DOS window opened to the folder containing QBasic.exe and myscript.bas, type the following:

    gwbasic /run myscript.bas

This loads and runs the program found in myscript.bas.

The extension ".bas" is normally used for QBasic source code files, but any filename extension can be used.

Also, the 8.3 file name convention must be used. QBasic does not recognize the long file names of modern Windows. If no suffix is provided, ".bas" is assumed.

Sample Program
This expanded example provides a better feel for what a complete QBasic program can do, as well as exposing some of its limitations. In this example, filenames in a directory are accessed and only those with "help" in the filename are printed.

    shell "dir *.* /b > temp.txt"
    open "temp.txt" for input as #1    
    do
       line input #1, filename$       
       if instr("help",filename$) then print filename$
    loop until eof(1)

And here's a line-by-line analysis of what the code does.

  • shell "dir *.* /b /l < temp.txt"
    Uses the DOS directory command with redirection to put a list of lower case file names into a file called temp.txt. This demonstrates the ability of QBasic to run external programs. It also demonstrates how limited are the functions QBasic has for dealing with files and directories. QBasic has no function to read files in a directory.

  • open "temp.txt" for input as #1
    Opens the file created in the previous step

  • do
    Starts the loop of reading each line in the file

  • line input #1, filename$
    Read the next line in the file

  • if instr(filename$, "help") then print filename$
    Checks to see if "help" is in the filename. Is so, the filename is printed

  • loop until eof(1)
    Continues the loop until the last line is read

When the last line of the script is executed, the QBasic program ends.

Tip: Capturing QBasic Output to a Text File
Print statements may be used to create a record of the output of a QBasic program. However, the text output of any QBasic program, even those not written to use print statements, may be saved to a file by using the redirect capabilities of MSDOS.

To save the output of a QBasic program, use the following approach:

    qbasic /run myscript.bas > output.txt
This loads the programs and automatically runs it. The redirection > symbol also tells the PC to place standard output into a file.