Confused of some code
Blitz3D Forums/Blitz3D Beginners Area/Confused of some code
| ||
Hi again. I know this is kinda of urmmmm silly question but i am reading a tutorail on Arrays and its gave an example... but I do not understand is how it works. Basicly it says this sets all the intergers to 0 For a=0 To 9 Cups(a)=0 Next I cant understand how that works, I thought loops was done that way.. Am I correct in saying that code basicly says: Ok.. For letter A will hold 0 to 9, amd cups(a) will now have empty or 0 in 0 to 9. Next do this till u reach 9 then hit next line of code. Is that basicly what its doing? Silly code but like i said I aint got no brains and I trying me best here. Cheers waz |
| ||
So I got this and it prints fook all :) Dim cups$(10) For a = 0 To 10 cups$(a) = "rat" Next Print cups$(a) |
| ||
ahh I think I worked it out. I cant do print cups$(a) to print out all the results so I did it like this Dim cups$(9) for a = 0 to 9 cups$(a) = "warren" Next Print cups$(0): Print cups$(1) etc etc But surely theirs a way to print em all instead of typing out prin: prin: print: etc constantly? |
| ||
ahhh there is silly me, still learning and bodging up. I can do For loop3 = 0 to 9 print cups$(loop3) next YAY! |
| ||
O.K. waz. I think you've got the basic gist of it, but arrays can be a bit confusing at first. Try this instead: Dim cups$(10) For a = 0 To 9 ; we've only got 10 elements in the array and 0 is a number. cups$(a) = "rat "+a Next Print cups$(5) ; display the contents of element 5 in the array cup$ (actually the 6th element. The reason why print cup$(a) wouldn't work is because the variable a is undefined once the for..next loop terminated. If you want to display the whole array, try this: Dim cups$(10) For a = 0 To 9 ; we've only got 10 elements in the array and 0 is a number. cups$(a) = "rat "+a Next for b=0 to 9 Print cups$(b) ; display the contents of element b in the array cup$. next I hope that makes sense. Have fun. |
| ||
Oh, you sorted while I was typing my reply. Ah well. |
| ||
urm u kinda confused me more. Whats the +A there for after rat? |
| ||
just to make the elements unique (rat 0, rat 1, etc..) |
| ||
> For a=0 To 9 > Cups(a)=0 > Next It's the loop bit. The a variable is the key here. Each loop it is different. a=0 the way up to 9. As it is placed inside Cups() it goes through all 0-9 of cups. a is confusing, but it could be anything. You could do this For badgers=0 To 9 Cups(badgers)=0 Next or For blahblahblah=0 To 9 Cups(blahblahblah)=0 Next or even For spoon=9 To 0 Step -1 Cups(spoon)=0 Next Which is the same as before but now the loop goes backwards starting from 9 and ending at 0. Adding Step -1 makes this happen as normally it is Step +1 |
| ||
Dim cups$(10) For a = 0 To 9 ; we've only got 10 elements in the array and 0 is a number. Dim whatever(10) will give you 11 elements! 0,1,2,3,4,5,6,7,8,9,10 Dim cheese(10) For n=0 To 10 cheese(n)=n Print cheese(n) Next |
| ||
doh! i'm a bit thick today :) |
| ||
For and Next are pretty confusing commands, especially in combination with arrays... For starts a counter loop and counts and loops until a number is reached... Hard definition.... But you could see it as this For K = 1 to 10 Print K Next The for loops starts on 1, and repeats the cycle, each time the cycle retuns the counter gets higher until it reaches 10, and K is in this case the counter that stores the value of it... The code shown here will do this 1 2 3 4 5 6 7 8 9 10 With arrays, things are a bit complicated, and I think you make things pretty difficult to mix these things when you're just starting out... As for your code For a=0 To 9 Cups(a)=0 Next Okay I take it you have done Dim Cups(9)... You have an array which can contain 9 values numbered from 0 to 9... All this code does is set them all to 0... In fact the way this code is executed is pretty semiliar to this Cups(0) = 0 Cups(1) = 0 Cups(2) = 0 Cups(3) = 0 Cups(4) = 0 Cups(5) = 0 Cups(6) = 0 Cups(7) = 0 Cups(8) = 0 Cups(9) = 0 But the for-next method is just way shorter... But I think you took on things too fast by mixing this right away... Perhaps you should first take the works of the FOR command and the working with arrays apart from each other... |
| ||
Check this out. I hope this simplifies it. Get 10 cups$ We're gonna count from 0-9. 'a' is going to hold our number cup$ number a = "cookie " + a move to the next number (a=a+1) ****************************************************** Your output would be cookie 0 cookie 1 cookie 2 cookie 3 cookie 4 cookie 5 cookie 6 cookie 7 cookie 8 cookie 9 hmmm... now if we only had some milk... |