#### Archived

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

# while loops

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

## 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 on other sites
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 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 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 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 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 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)

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
frob
13

• 9
• 14
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007359

×