Qbasic, Quickbasic/read statements

Advertisement


Question
could you please explain read and data statements and how to use them.  i have a very usless hardass of a teacher and im lost
thanks

Answer
Hi, corndog (nice nickname),

I can definitely help you understand READ and DATA statements.  A data statement is a set (or list) of data that is inside your program, that you can read at any point in time, and place into variables.  Lets say we wanted our data statement to list the numbers 1 to 5, it would look like this:

DATA 1, 2, 3, 4, 5

Notice that all information is separated by commas.  You could also write it like this:

DATA 1
DATA 2
DATA 3
DATA 4
DATA 5

Both, according to the read statement, will be exactly the same.

Now, to read one number, you would do this:

DIM number AS INTEGER

READ number
PRINT number

' ...

DATA 1, 2, 3, 4, 5

The output from the print should be "1" (without quotes)

Now, before I continue, I should say why you would want to use a DATA/READ system in your programs.  Well, there are two main reasons:  the first, being, that the data will stay constant (unless someone changes your code).  The second is that you will always be aware, as the programmer, of how much data will be read.

Okay, so, you're probably wondering how you read all of the data.  If you simply wanted to read and output it without storing it, this is how I would do it:

DIM number AS INTEGER
DIM counter AS INTEGER

FOR counter = 1 TO 5
 READ number
 PRINT number
NEXT counter

' I have changed the values here, just so you can see that the numbers aren't affected by the FOR/NEXT loop.  I'm still using 5 numbers, though.
DATA 10, 4, 438, 22, 80

If you needed to keep those numbers for later, you would want to make an array ( DIM number(1 to 5) AS INTEGER ) and make the read statement say: READ number( counter )  Now the number( index ) will correspond to the data numbers.

For the data, you can use anything, from integers, singles, doubles, strings, etc.  Whatever sort of data you want.  It could look like this:

DATA "Hello World", 42, 3.141596, 0.5, "A string with numbers in it: 12345"

I hope that helps explain things.  If there is still something you don't understand, just ask specifically, and I'll be more than happy to get specific in one aspect or another.

Take care,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.