Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Xtremehobo

while loops

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve got this while loop:
int freeblocks = 2;
while(1)
{
  if(freeblocks > 0)
  {
    freeblocks--;       
    WriteCrapToVCPPDebug("test\n");
  }
}
how come freeblocks > 0 is ALWAYS True while it loops even though freeblocks--; should make freeblocks <= 0 after two iterations? I''ve tried making freeblocks an unsigned int, that didn''t work. I also tried replacing freeblocks--; with:
if(freeblocks==1)
  freeblocks=0;
if(freeblocks==2)
  freeblocks=1;
and the if(freeblocks>0) is still always true. Thanks, Matt Matt Carpenter Founder and CEO of nothing! Deviantart | My Site

Share this post


Link to post
Share on other sites
Advertisement
How do you know it is always true? Do you get lots of ''test'' in your log?
You are never breaking out of the loops even when freeblocks is less than or equal to zero. You probably want:
int freeblocks = 2;
while(freeblocks > 0)
{
freeblocks--;
WriteCrapToVCPPDebug("test\n");
}
Or, if you must keep your current format with the if inside the loop, you need to add an "else break;" after the if so it exits the loop.

Share this post


Link to post
Share on other sites
quote:
Original post by Extrarius
How do you know it is always true? Do you get lots of ''test'' in your log?
You are never breaking out of the loops even when freeblocks is less than or equal to zero. You probably want:
int freeblocks = 2;
while(freeblocks > 0)
{
freeblocks--;
WriteCrapToVCPPDebug("test\n");
}
Or, if you must keep your current format with the if inside the loop, you need to add an "else break;" after the if so it exits the loop.


Yep, I get lots of tests in the log. I do not want to do while(freeblocks > 0) however, because some of my functions that Im sticking in the loop will increment freeblocks periodically.

Share this post


Link to post
Share on other sites
What difference does it make if it increments it? The loop will run as long as the variable is above 0, which from your example is what you want. Unless, of course, your exit condition is something other than that if statement in which of course you are right =-P

If it is always greater than 0, then the problem might be that it is incrementing it when it shouldn't be so it never does go below 0. Have you tried outputting the value of the variable so you can see if that is the case? Or use a debugger and watch the variable in the loop?

[edited by - extrarius on February 27, 2004 11:28:12 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Extrarius
Unless, of course, your exit condition is something other than that if statement in which of course you are right =-P


actually my while statement is more like while(offset - blocksize [less than] filesize)

quote:

If it is always greater than 0, then the problem might be that it is incrementing it when it shouldn't be so it never does go below 0. Have you tried outputting the value of the variable so you can see if that is the case? Or use a debugger and watch the variable in the loop?



I suppose I'll look at it in the debugger but I havn't enabled the stuff inside the if statement that increments it yet.


[edited by - Xtremehobo on February 27, 2004 11:35:06 PM]

Share this post


Link to post
Share on other sites
hmmmmmmm... that''s interesting, when I run it in debug mode without any breaks, it gives me lots in the debug console, but when I break and run it step-by-step everything runs as it should...

Share this post


Link to post
Share on other sites
You might want to check any arrays you''re using to make sure you aren''t going outside the bounds. Every time I have a problem like you''re describing, its almost always caused by out of bounds array access (or dereferencing pointers that aren''t properly initialized)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!