Monday, 2024-05-06, 9:20 PM

Welcome Guest | RSS

MainRegistrationLogin
Qbasic Home

Catalog categories
Qbasic Tutorial [31]

Rate my Site
Rate JPSor Site
Total of answers: 168

Main » Articles » Qbasic Tutorial

Chapter 28 - Qbasic Tutorial

Loading an array with a lot of values can get pretty boring.

 ' Load up the array
A(0) = 10
A(1) = 24
A(2) = 31
A(3) = 15
A(4) = 67
A(5) = 34
A(6) = 87
A(7) = 92
A(8) = 14
' ... This could go on for quite a while

FOR I = 0 TO 8
PRINT A(I)
NEXT I

To save space, and typing, QBASIC provides the DATA and READ statements. You can place a list of data values in a DATA statement and read them into an array using READ. The following program will do exactly what the previous program does.

 DATA 10, 24, 31, 15, 67, 34, 87, 92, 14

FOR I = 0 TO 8
READ A(I)
NEXT I

FOR I = 0 TO 8
PRINT A(I)
NEXT I

QBASIC skips the DATA statements in the code. It only pays attention to them when it finds a READ statement. Each time a READ happens, QBASIC takes a value from the DATA statement and places it in the variable in the READ statement.

RESTORE

RESTORE tells QBASIC which DATA statement to start READing from. You might need to load several different arrays in your program. RESTORE lets you organize the DATA statements any way you want.

 Names:
DATA Fred, Joe, Jack, Sue
Values:
DATA 10, 24, 31, 15, 67, 34, 87, 92, 14

' Start with the DATA statement after "Values:"
RESTORE Values
FOR I = 0 TO 8
READ A(I)
NEXT I

' Start with the DATA statement after "Names:"
RESTORE Names
FOR I = 0 TO 8
READ N$(I)
NEXT I

RESTORE tells QBASIC to start reading at the next DATA statement after a label. A label is a name like "Names:" or "Values:". Notice that when we make a new label, we use a colon ":", but when we use the label in the RESTORE statement, we drop the colon.

Loading a Database

DATA statements are perfect for loading a database. Here's a new version of the database example using DATA statements this time.

 DATA "Joe Blow", "1-310-555-1212"
DATA "Jack Sprat", "1-340-555-6545"
DATA "Carol Christmas", "1-350-555-2421"

TYPE FriendType
FullName AS STRING * 20
PhoneNumber AS STRING * 14
END TYPE

' The database
DIM Friends(2) AS FriendType

' Read in the database from the DATA statements
FOR I = 0 TO 2
READ Friends(I).FullName, Friends(I).PhoneNumber
NEXT I

' Print out the entire database
FOR I = 0 TO 2
PRINT Friends(I).FullName; ": "; Friends(I).PhoneNumber
NEXT I


Source: http://jpsor.ucoz.com
Category: Qbasic Tutorial | Added by: JPSor (2009-02-26) | Author: JPSor
Views: 4571 | Rating: 4.3/3 |
Login

Qbasic Clock

Search

Visit my Friends

Who's Online

Total online: 1
Guests: 1
Resgistered: 0


Copyright MyCorp © 2024