View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# for loop skipping to the end after first loop

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Killerko  Members

Posted 14 June 2014 - 06:04 PM

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, 14 June 2014 - 06:05 PM.

### #2phil_t  Members

Posted 14 June 2014 - 06:22 PM

POPULAR

Where in the loop did you put the breakpoint? Maybe your if statement only holds true for i =0 or i = 4 (or -2, 2).

### #3SeanMiddleditch  Members

Posted 14 June 2014 - 06:31 PM

POPULAR

Learn more about your debugger. A breakpoint ("the red dot") is just one tool. You can also Step the code, letting you follow along a loop and inspect the value of a variable at every line. There's also the ability to set data breakpoints allowing you to detect when a variable changes no matter which line of code actually does it. And plenty more.

phil_t is probably right, though. Your code looks fine and that seems a reasonable explanation for the behavior you're seeing. The other possibility is that you are misusing one of your arrays (either you made it too small or your indexing calculations are wrong) and you're clobbering stack variables (like i) when you try to write to the array. The data breakpoint feature will help find those cases (if i changes when you try to write to an array, something is wrong with the array or your use of it).

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

### #4Killerko  Members

Posted 14 June 2014 - 06:31 PM

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!

### #5Killerko  Members

Posted 14 June 2014 - 06:40 PM

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.

### #6Lactose  GDNet+

Posted 14 June 2014 - 07:05 PM

POPULAR

I'm not sure what application you're using to run/debug this in, but I think the following is true for most major IDEs.

A breakpoint will stop the application at the indicated line. From here, you can look at various variables, resume the application, or step through the code.

For stepping through code, you mainly have 2 things -- step over and step into. Step over will continue to the next line in the code, while step into will take you deeper into the code if possible. Basically, on the following line:

int x = getPositionFromCharacter(myCharacter); //Breakpoint here
int y = 15;


Step over will go to the "int y" line, while step into will go into the first line of the "getPositionFromCharacter" function. After stepping through the entire function (or using step out), you'll exit the function and be back where you entered (in this case, the "int x" line).

Common keyboard shortcuts for these are:

F9: Set breakpoint at currently selected line.

F10: Step over.

F11: Step into.

Shift+F11: Step out.

You can also have conditionals for breakpoints, the two I use the most being:

- Only break if the value is a specific thing or if the value changes (conditional breakpoint),

- Only break on certain hits, e.g. after 2 hits, or ever 6 hits, etc.

In Visual Studio, you can right click on the breakpoint spot to set various types of breakpoints.

In addition, you can also assign variables to a watch list. This allows you to keep track of what the variable is easily. For example, you could add a specific array position (array + index) to watch, and be able to quickly see if it changes in a separate listing (basically saving you having to track it down every time you want to see its value).

Hope that helps

Hello to all my stalkers.

### #7Killerko  Members

Posted 14 June 2014 - 07:48 PM

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.

### #8SeanMiddleditch  Members

Posted 15 June 2014 - 01:41 AM

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.