Qbasicnews.com

Full Version: Array's & FOR...NEXT Questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have some questions that need to be answered.

I still don't understand array's very much, what are they useful for?
The FOR...NEXT statements I'm pretty sure how it works but I'd like a better example other then:

Code:
For i = 1 TO 10
PRINT "The Number"; i
NEXT i

I tried to put a string "$" value of a Array DIM in place of "i" & fooled around to see if it could PRINT out per line each of the 10 Variables inside the Array. I worked on & off all morning yesterday, but still couldn't get it to work.

I just sarted Qbasic for the first time yesterday & have been reading Qbasic Lessons. (Right now I'm on chapter 4.) I did the Calendar Exercise in chapter 3, but I believe that if I can learn/master the FOR...Next command I probably could lessen the code I have already written for my calendar program.

On a last note, I am new to Qbasic & this forum so I'd like to say "Hello" to everybody out there.
Oftentime when you write complex programs, you need a lot of data. For instance, let's say you're storing a graphic in memory, and want to deliver it to the screen (QBs statements aside). Without an array, you're stuck doing soemthing like this:

pixel1 = 0
pixel2 = 1
pixel3 = 5

...etc...

Now if you were to do this, even for a tiny 16x16 graphic you'd have 256 variables and statements for anything you do with that graphic. However, let's say you were to do this:

DIM pixel(0 to 255)
for x = 0 to 15
for y = 0 to 15
pset (x, y), pixel(y * 16 + x)
next y
next x

think about 6 lines of code versus Lord knows how many. This is what arrays are good for, when you have a lot of data, too much to handled conveniently individually.

now try this:
Code:
dim x(1 to 10) as string * 50 ' i can never remember if this is necessary in arrays
for y = 1 to 10
x(y) = str$(y) + " bottles of beer on the wall"
next y
you have an array that looks like this:
x(1) = "1 bottles of beer on the wall"
x(2) = "2 bottles of beer on the wall"
x(3) = "3 bottles of beer on the wall"
x(4) = "4 bottles of beer on the wall"
x(5) = "5 bottles of beer on the wall"
x(6) = "6 bottles of beer on the wall"
x(7) = "7 bottles of beer on the wall"
x(8) = "8 bottles of beer on the wall"
x(9) = "9 bottles of beer on the wall"
x(10) = "10 bottles of beer on the wall"

now try this:
Code:
for y = 1 to 10
print x(y)
next y

remember, that when this executes, the code inside the for...next loop repeats itself 10 times, and every time y is a different value.
Quote:dim x(1 to 10) as string * 50 ' i can never remember if this is necessary in arrays

Nope, only in TYPEs


oh, and hi Gin, welcome to the QBN forums, I hope we can help you with any problems/questions you have.
Quote:On a last note, I am new to Qbasic & this forum so I'd like to say "Hello" to everybody out there.


Greetings! Big Grin
Quote:On a last note, I am new to Qbasic & this forum so I'd like to say "Hello" to everybody out there.

Cheers dude :bounce:

and not to say that Mallards lessons that you are reading are bad or anything but I also suggest that you take a peek at Vic Luce's tutorials if you havent already, they're really great, check them out here

[url]
http://www.qbasicnews.com/tutorials.php

[/url]
What's up jofers, dark_prevail, Novaprogramming, & Offensive Screenname.

Jofers I now understand why I need DIM array's, after reading about SUB's & Functions in Qbasic Classes Chapter 5. Should have just skipped ahead, but I didn't want to rush things by throwing this information in my head all at once. (easy steps grasshopper, ha ha ha) :bounce:

I still have some questions on the FOR...Next command, but will ask again tomorrow. Since I'm too tried out from study & writing code all these hours.

By the way Offensive Screenname your Vic Luce URL was a really great help to me. I've skimmed threw the first 5 tutorials & have reread 1 - 3. It sure feels good when I've wrote code that actually shows results on the screen in a fun faction.

I have one question though, can you write online multiplayer games in Qbasic?
No, QB doesn't do multitasking and it would be a miracle if a QB program ran online

Btw. Welcome at the boards Smile
Yes, you can.
I have a Web server someone made in QB but it doesn't work.
Quote:No, QB doesn't do multitasking and it would be a miracle if a QB program ran online

You dont need multitasking to do a multiplayer game... And QB online has been done... kind of.
Pages: 1 2