AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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, c/c++, python, 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
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > read statements

Qbasic, Quickbasic - read statements


Expert: Alex Barry - 10/13/2009

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

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.