QuickBASIC Selected Programs

Worth knowing

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

QuickBASIC Selected Programs

There are six sets of example programs, each containing several programs. Each program is 10 to 20 lines long and carries out a function that is often used in larger programs. For example, one of the File I/O programs creates a new sequential data file on disk, accepts input from the keyboard, and writes that data to the file - all with only 13 program lines.

These example programs differ from the programming examples that are available for each BASIC keyword through on-line help. These programs are more generally useful, and are not written to demonstrate the use of one statement or function.

They also differ from the sample application programs such as INDEX.BAS and TERMINAL.BAS. Those programs are much larger and more complex.

  • The following programming examples illustrate:
  • how to code loops and decision-making program structures
  • the similarities and differences between the SELECT CASE and IF...THEN...ELSE statements
Examples

Here is an example of using 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

The above decision is rewritten using SELECT CASE below:

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

The following decision can be made with either SELECT CASE or the block IF...THEN...ELSE statement. The comparison is more efficient with the block IF...THEN...ELSE statement because different expressions are being evaluated in the IF and ELSEIF clauses.

INPUT X, Y IF X = 0 AND Y = 0 THEN PRINT "both zero." ELSEIF X = 0 THEN PRINT "only X is zero." ELSEIF Y = 0 THEN PRINT "only Y is zero." ELSE PRINT "neither is zero." END IF

The following programming examples illustrate how to code file I/O and device I/O statements.
Examples

The following short program creates a sequential file named "Price.Dat", then adds data entered at the keyboard to the file. The OPEN statement in this program both creates the file and readies the file to receive records. The WRITE # then writes each record to the file. Note that the number used in the WRITE # statement is the same number given to the file name Price.Dat in the OPEN statement.

' Create a file named Price.Dat ' and open it to receive new data: OPEN "Price.Dat" FOR OUTPUT AS #1 DO ' Continue putting new records in Price.Dat until the ' user presses ENTER without entering a company name: ' INPUT "Company (press <<ENTER>> to quit): ", Company$ IF Company$ <> "" THEN ' Enter the other fields of the record: INPUT "Style: ", Style$ INPUT "Size: ", Size$ INPUT "Color: ", Clr$ INPUT "Quantity: ", Qty ' Put the new record in the file ' with the WRITE # statement: WRITE #1, Company$, Style$, Size$, Clr$, Qty END IF LOOP UNTIL Company$ = "" ' Close Price.Dat (this ends output to the file): CLOSE #1 END

The following program opens the Price.Dat file created in Example 1 above and reads the records from the file, displaying the complete record on the screen if the quantity for the item is less than the input amount.
The INPUT #1 statement reads one record at a time from Price.Dat, assigning the fields in the record to the variables Company$, Style$, Size$, Clr$, and Qty. Since this is a sequential file, the records are read in order from the first one entered to the last one entered.
The EOF (End Of File) function tests whether the last record has been read by INPUT #. If the last record has been read, EOF returns the value 1 (true), and the loop for getting data ends; if the last record has not been read, EOF returns the value 0 (false), and the next record is read from the file.

OPEN "Price.Dat" FOR INPUT AS #1 INPUT "Display all items below what level"; Reorder DO UNTIL EOF(1) INPUT #1, Company$, Style$, Size$, Clr$, Qty IF Qty < Reorder THEN PRINT Company$, Style$, Size$, Clr$, Qty END IF LOOP CLOSE #1 END

The WRITE # statement can be used to write records to a sequential file. There is, however, another statement you can use to write sequential file records:PRINT #The best way to show the difference between these two data-storage statements is to examine the contents of a file created with both. The following short fragment opens a file Test.Dat then places the same record in it twice, once with WRITE # and once with PRINT #. After running this program you can examine the contents of Test.Dat with the DOS TYPE command:

OPEN "Test.Dat" FOR OUTPUT AS #1 Nm$ = "Penn, Will" Dept$ = "User Education" Level = 4 Age = 25 WRITE #1, Nm$, Dept$, Level, Age PRINT #1, Nm$, Dept$, Level, Age CLOSE #1

The following programming examples demonstrate various ways to manipulate sequences of ASCII characters known as strings.
Examples

One of the most common string-processing tasks is searching for a string inside another string. The INSTR function tells you whether or not string2 is contained in string1 by returning the position of the first character in string1 (if any) where the match begins. If no match is found (that is, string2 is not a substring of string1, INSTR returns the value 0.
The following programming example demonstrates this:

String1$ = "A line of text with 37 letters in it." String2$ = "letters" PRINT " 1 2 3 4" PRINT "1234567890123456789012345678901234567890" PRINT String1$ PRINT String2$ PRINT INSTR(String1$, String2$)

Sample Output:

1 2 3 4 1234567890123456789012345678901234567890 A line of text with 37 letters in it. letters 24

The INSTR(position, string1, string2) variation is useful for finding every occurrence of string2 in string1, instead of just the first occurrence of string2 in string1, as shown in the next example:

String1$ = "the quick basic jumped over the broken saxophone." String2$ = "the" PRINT String1$ Start = 1 NumMatches = 0 DO Match = INSTR(Start, String1$, String2$) IF Match > 0 THEN PRINT TAB(Match); String2$ Start = Match + 1 NumMatches = NumMatches + 1 END IF LOOP WHILE MATCH PRINT "Number of matches ="; NumMatches

Sample Output:

the quick basic jumped over the broken saxophone. the the Number of matches = 2

The following program shows how to trap an event that occurs while a program is running. With event trapping, you can get your program to detect run-time events such as keystrokes.
Example

If you use a 101-key keyboard, you can trap any of the keys on the dedicated keypad by assigning the stringCHR$(128) + CHR$(scancode)to any of the keynumber values from 15 to 25.

The next example shows how to trap the LEFT direction keys on both the dedicated cursor keypad and the numeric keypad.

' 128 = keyboard flag for keys on the ' dedicated cursor keypad ' 75 = scan code for LEFT arrow key ' KEY 15, CHR$(128) + CHR$(75) ' Trap LEFT key on ON KEY(15) GOSUB CursorPad ' the dedicated KEY(15) ON ' cursor keypad. ON KEY(12) GOSUB NumericPad ' Trap LEFT key on KEY(12) ON ' the numeric keypad. DO LOOP UNTIL INKEY$ = "q" ' Idle loop END CursorPad: PRINT "Pressed LEFT key on cursor keypad." RETURN NumericPad: PRINT "Pressed LEFT key on numeric keypad." RETURN

The following programming examples create shapes, colors, and patterns on your screen.
Example

By making either of the CIRCLE statement start or end arguments negative, you can connect an arc at its beginning or ending point with the center of a circle. By making both arguments negative, you can draw shapes ranging from a wedge that resembles a slice of pie, to the pie itself with the piece missing. This example draws a pie shape with a piece missing.

SCREEN 2 CONST RADIUS = 150, PI = 3.141592653589# StartAngle = 2.5 EndAngle = PI ' Draw the wedge: CIRCLE (320, 100), RADIUS, , -StartAngle, -EndAngle ' Swap the values for the start and end angles: SWAP StartAngle, EndAngle ' Move the center 10 pixels down and 70 pixels to the ' right, then draw the "pie" with the wedge missing: CIRCLE STEP(70, 10), RADIUS, , -StartAngle, -EndAngle

Sample Output:

You'll have to run the program to see the output.

The following programming example illustrates how to code and combine small logical components of programs.
Example

Corresponding variables must have the same type in both the argument and parameter lists for a function, as shown in the following example. In this example, two arguments are passed to the FUNCTION procedure. The first is an integer specifying the length of the string returned by the FUNCTION, while the second is a character that is repeated to make the string.

FUNCTION CharString$(A AS INTEGER, B$) STATIC CharString$ = STRING$(A%, B$) END FUNCTION DIM X AS INTEGER INPUT "Enter a number (1 to 80): ", X INPUT "Enter a character: ", Y$ ' Print a string consisting of the Y$ character, repeated ' X number of times: PRINT CharString$(X, Y$) END

Sample Output:

Enter a number (1 to 80): 21 Enter a character: # #####################