Public Group

# for loop skipping to the end after first loop

This topic is 1495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I have strange problem with a standard for loop..

It should go from -2, to 2 (-2, -1, 0, 1, 2) but when I debug the program and place the red dot inside the loop and look at the "i" value it goes from -2 directly to 2 after first loop. (-2, 2) which is puzzling me why?

here is the loop:


for(int i = -2; i < 3; i++)
{
if(myMap[(y-2) * myMapW + x+i].properties != 0 && myMap[(y-1) * myMapW + x+i].properties != 1)
{
tileTemp.sprite = 1;//if(getTileSprite(x+i, y-2) > 2) tileTemp.sprite = rand()%2 + 1;
tileTemp.entity = this->getTileEntity(x+i, y-2);
tileTemp.properties = 2;
myMap[(y-2) * myMapW + x+i] = tileTemp;
}
}



I tried to change the negative numbers so the loop should go from 0, 1, 2, 3 and 4 but it goes from 0 to 4 right after first loop... why?

for(int i = 0; i < 5; i++)
{
if(myMap[(y-2) * myMapW + x+i-2].properties != 0 && myMap[(y-1) * myMapW + x+i-2].properties != 1)
{
tileTemp.sprite = 1;//if(getTileSprite(x+i-2, y-2) > 2) tileTemp.sprite = rand()%2 + 1;
tileTemp.entity = this->getTileEntity(x+i-2, y-2);
tileTemp.properties = 2;
myMap[(y-2) * myMapW + x+i-2] = tileTemp;
}
}

I use visual studio 2010 and for loop so far have worked as it should.. well, until now

Edited by Killerko

##### Share on other sites

yes thats it.. thanks phil_t! there is a mistake in the second part of the if statement..

"myMap[(y-1)" should be "myMap[(y-2)" and thats exactly why it turned false for 1-3 (-1,1)  ..

thanks! :)

##### Share on other sites

SeanMiddleditch: I'm pretty new to this and still learning mostly.. all I can do while debuging is to place these breakpoints and when the program stops I can examine my variables and they values.. then let the program continue and wait for another stop to examine values again.

How do you use those data breakpoints? that sound like something that could be very useful.

##### Share on other sites

Thank you Lactose, all this is very helpful indeed!

I've seen those step over and step into icons on the debugging toolbar in my visual studio but had no idea how to use them so I've never used them before.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631748
• Total Posts
3002030
×