while loops

Started by
5 comments, last by Xtremehobo 20 years, 1 month ago
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
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
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.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
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]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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]
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
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...
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
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)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement