# SOLVEDIssues with mod function in for loop

#### Papa Doge

##### Member
I'm definitely scratching my head with this one and not sure why it's failing:

Code:
``````for (var l=1; l<((bc*br)+1); l++;) {
biome_grid_positions_list[| l] = [bx, by];
if (l mod bc == 0.00) {
bx = x;
by += cell_size;
show_debug_message("Wrapping here at column " + string(bc) + " and grid position " + string(l));
} else {
bx += cell_size;
}
show_debug_message("Grid position at " + string(l) + " and l mod bc is " + string(l mod bc));
}``````
When I check the debug messages this works as intended until it needs to wrap to row 6 at grid position 138. It shows that 138 / 6 = 0.00 which should be resetting my x position and moving down but it's not and I'm not sure what's breaking here.

#### FrostyCat

##### Member
For starters, count from 0 instead of 1, like a programmer should. Like a lot of other things involving counting in code, "calendar wraps" using div and mod only work cleanly when zero-indexed.
GML:
``````var imax = bc*br;
for (var i = 0; i < imax; ++i) {
bx = x+cell_size*(i mod bc);
by = y+cell_size*(i div bc);
}``````

#### Nidoking

##### Member
Shouldn't you be resetting by when you increase bx?

This seems like the kind of loop that's better written as a pair of nested for loops on bx and by rather than a single one. Go from bx = 1 to bc and by = 1 to br, and increment l each time through.

#### Papa Doge

##### Member
I had a god chuckle at that post. Thanks for the slap on the knuckles. Your solution works like a charm and is much more efficient than what I was trying to do. Thanks for the assist.