IGNORED

# using for-next statements

## Recommended Posts

Suppose I've got a set of 2 data like this:

``` data xdata

28, 52, 76, 100, 124

end

data ydata
12, 50, 86
end```

How would I then access all 5 data in xdata and all 3 data in ydata at the same time? Would it be something like this:

if x=xdata[o] && y=ydata[p] then ----whatever

then how would I set o to equal 0, 1, 2, 3, and 4; and p to equal 0, 1, and 2? Using a for statement? I've looked through the help file and didn't understand anything about for-next statements, and I don't even know what a "step" is. Help!

Edited by atari2600land
##### Share on other sites

Suppose I've got a set of 2 data like this:

``` data xdata

28, 52, 76, 100, 124

end

data ydata
12, 50, 86
end```

How would I then access all 5 data in xdata and all 3 data in ydata at the same time? Would it be something like this:

if x=xdata[o] && y=ydata[p] then ----whatever

then how would I set o to equal 0, 1, 2, 3, and 4; and p to equal 0, 1, and 2? Using a for statement? I've looked through the help file and didn't understand anything about for-next statements, and I don't even know what a "step" is. Help!

Why would you have 5 in the x and only 3 in the y?

##### Share on other sites

Why would you have 5 in the x and only 3 in the y?

This is the playfield I'm working with:

##### Share on other sites

I've looked through the help file and didn't understand anything about for-next statements, and I don't even know what a "step" is.

To answer your last question first, a "for...next" statement is a kind of loop that lets you perform a set of instructions a certain number of times, based on the value of a particular variable. Each time the program goes through the loop, that particular variable will have a particular value, and its value will be incremented or decremented by a particular amount after each pass through the loop, based on the "step" value specified. For example, consider the following "for...next" loop:

```   for i = 1 to 10 step 2
pfpixel i i on
next```

This is functionally equivalent to the following:

```   i = 1
loop
pfpixel i i on
i = i + 2
if i <= 10 then goto loop```

Both of these code snippets will do the same thing-- they will turn on the playfield pixels at coordinates (1,1), (3,3), (5,5), (7,7), and (9,9). So the generalized format of a "for...next" loop, and the equivalent "goto" loop, would be as follows:

```   for variable = starting_value to ending_value step increment_or_decrement_amount
rem do something here
next
rem continue program```

```   variable = starting_value
loop_label
rem do something here
if variable = ending_value then goto exit_label
variable = variable + increment_or_decrement_amount
goto loop_label
exit_label
rem continue program```

Suppose I've got a set of 2 data like this:

``` data xdata

28, 52, 76, 100, 124

end

data ydata
12, 50, 86
end```

How would I then access all 5 data in xdata and all 3 data in ydata at the same time? Would it be something like this:

if x=xdata[o] && y=ydata[p] then ----whatever

then how would I set o to equal 0, 1, 2, 3, and 4; and p to equal 0, 1, and 2? Using a for statement?

That depends on how you want to access the two data tables-- i.e., how you want to use them. Typically, you wouldn't want to use a "for...next" loop unless you want to perform some code a particular number of times, or you want to vary an index or other variable from one (starting) value through another (ending) value. For example, consider the following code:

```   for o = 0 to 4
for p = 0 to 2
if x = xdata[o] && y = ydata[p] then gosub do_whatever
next
next```

That would be equivalent to doing the following:

```   if x = xdata[0] && y = ydata[0] then gosub do_whatever
if x = xdata[0] && y = ydata[1] then gosub do_whatever
if x = xdata[0] && y = ydata[2] then gosub do_whatever
if x = xdata[1] && y = ydata[0] then gosub do_whatever
if x = xdata[1] && y = ydata[1] then gosub do_whatever
if x = xdata[1] && y = ydata[2] then gosub do_whatever
if x = xdata[2] && y = ydata[0] then gosub do_whatever
if x = xdata[2] && y = ydata[1] then gosub do_whatever
if x = xdata[2] && y = ydata[2] then gosub do_whatever
if x = xdata[3] && y = ydata[0] then gosub do_whatever
if x = xdata[3] && y = ydata[1] then gosub do_whatever
if x = xdata[3] && y = ydata[2] then gosub do_whatever
if x = xdata[4] && y = ydata[0] then gosub do_whatever
if x = xdata[4] && y = ydata[1] then gosub do_whatever
if x = xdata[4] && y = ydata[2] then gosub do_whatever```

So whether you'd want to use a "for...next" statement for that particular example you posted would depend on whether or not you wanted it to behave as shown in the equivalent code.

Michael

##### Share on other sites

Why would you have 5 in the x and only 3 in the y?

This is the playfield I'm working with:

Okay, so you are thinking about using two data tables to define which rows and columns of the maze you can place a player in, so the player doesn't end up being stuck inside one of the contained areas? That makes sense, but a pair of nested "for...next" loops might take up more execution time than you'd prefer, and I don't think you'd need to check both tables at once, unless you were trying to find the intersections in the maze.

```   dim is_this_a_safe_spot = a
is_this_a_safe_spot = 0 : rem initialize it to "no" ("false," or 0)
for b = 0 to 4
if x = xdata[b] then is_this_a_safe_spot = 1 : rem yes it is
next
for b = 0 to 2
if y = ydata[b] then is_this_a_safe_spot = 1 : rem yes it is
next```

Michael

##### Share on other sites

I ended up using the first example, as I was pressed for space (was trying to make a 2k game.) The game I made will be in my blog later today. Thank you, Michael.

##### Share on other sites

It's better to find a formula that accomplishes the checks (in this case, checking for valid spots where 90 deg. direction changes are possible). How about changing 86y to be 88y? Then you can check for remainders (//) to see if the direction wanted is allowed...

temp2 = (X+20)//24

if temp1 = 0 then gosub y_ok

temp2 = (Y+26)//38

if temp1 = 0 then gosub x_ok

The subroutine x_ok should check the stick only for horizontal movement...as it's OK to move in that direction.

Likewise, y_ok checks the stick only for vertical movement.

Bb docs mention that you need to include div_mul16.asm for the remainder function to work.

Not versed in Bb logic, so this could probably be simpler.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.