# i broke while

## Recommended Posts

Cibressus    100
for some reason, my while loop no longer works, no matter what code i do, but dowhile still works. any insight?

##### Share on other sites
fredizzimo    133
If you post your code, then we can tell what you are doing wrong.

Maybe you have a semicolon after you while statement by mistake? Only the do...while version should be terminated by a semicolon.

I can't make a guess other than that, without seeing any code.

##### Share on other sites
Cibressus    100
no, no matter what i put

for example

while(1)
{
printf("this");
}

simply does not loop, i've had another programer check it to make sure i'm not going insane.

##### Share on other sites
doynax    850
Quote:
 Original post by Cibressusfor some reason, my while loop no longer works, no matter what code i do, but dowhile still works. any insight?

There's not much to say about do { } while and while except that they perform two different functions.

edit: 23 seconds too late =)

##### Share on other sites
Guest Anonymous Poster
Cibressus, are you serious?

##### Share on other sites
fredizzimo    133
Quote:
 Original post by Cibressusno, no matter what i putfor examplewhile(1){printf("this");}simply does not loop, i've had another programer check it to make sure i'm not going insane.

So if you replace that code with
do{printf("this");}while(1);

without changing anything else in the program, it loops?

I'm pretty sure you have something else wrong a few lines above the loop.

##### Share on other sites
doynax    850
Quote:
 Original post by Cibressusno, no matter what i putfor examplewhile(1){printf("this");}simply does not loop, i've had another programer check it to make sure i'm not going insane.

Exactly what happens? Does the program get stuck without generating any output, do you get a compiler error, does it print the first line and then exit or something else entirely?
That piece of test code might not generate any output because of c's line buffering. Try adding a newline.
Have you tried writing a separate test application consisting only of an infinite loop?

##### Share on other sites
S1CA    1418
Taking the "while (1) doesn't work case" as the basis for investigation:

1) What does it do when you single step it? I suspect infinity...

2) Try putting some non-important code after the while loop, put the cursor on that code and then do a "run to cursor" - is it ever getting there?

3) Bear in mind that the stdio streams have buffers and buffer limits - although "this" may have stopped appearing in your output/console window, it doesn't mean the loop itself has stopped! Try flushing the stream and adjusting the buffer.

4) Look at the assembly listing, see if it makes sense - every compiler I've worked with hasn't got anything as trivial as "while (1)" wrong in any way!!!

5) If the original cause for this post was a while loop involving floating point values, then what epsilon are you using? (if the answer to that is "epsilon, what?", then that's your problem...)

6) Somebody doing something stupid like #define'ing "1" or "while" would cause funnies (have only ever had to do anything so extreme in very rare debugging scenarios though!)

7) Is there a multi threaded element to this? if so, what synchronisation objects are you using, and how? A while loop checking a flag which is owned by another thread can very easily mis-generate code (often as bad as self:jmp self) without warning. If this is the case, try declaring shared variables as "volatile", and ideally use proper/safe synchronisation objects.

##### Share on other sites
felisandria    739
Is it possible to overload "while"? *ponder*

-fel

##### Share on other sites
S1CA    1418
Quote:
 Original post by felisandriaIs it possible to overload "while"? *ponder*-fel

Yes(ish): #define...

I've had to do it to help trace a program lock-up that only occurred in release builds. Someone on the team favoured while loops which compared float values for equality (GRRR!!) - the iteration count of the suspect while loops shouldn't have been more than 5 or 6, so I #define'd a version of while that was cause a breakpoint if it went over 1000 iterations and was #undef'd outside of the file under investigation.

##### Share on other sites
Cibressus    100
it simply freeses whenever it gets to a while loop.

##### Share on other sites
fredizzimo    133
In which way does it freeze? The example you gave is an infinite loop, so it will never end and therefore appear to be frozen.

Or does it simply not print out any "this" at all?

##### Share on other sites
Cibressus    100
it simply does not print out anything, it litterly freezes, it does nothing. it just stops.

##### Share on other sites
I've had weird things like that happen to me too when using Visual Studio 6. The common fix seems to be just create a new workspace and file and what not, and just copy and paste your code over to the new files and try it again.

Hope that helps.

##### Share on other sites
Zahlman    1682
Have you tried it with a \n after the 'this'? It may not be getting a chance to flush the buffer to console.

##### Share on other sites
thedevdan    210
Why don't you post an example program (run the program, and post it)? If the one you already posted does that, then... wow. But you HAVE to run it first, there may be something else.

##### Share on other sites
Ainokea    435
Does it work on any other projects? If not then reinstall your compiler.

Dont worry you still have for loops and as we all know:

for > while

Some people are langauge nazis, well I am a keyword nazi.
j/k.

##### Share on other sites
Roboguy    794
Quote:
 Original post by ZahlmanHave you tried it with a \n after the 'this'? It may not be getting a chance to flush the buffer to console.

\n doesn't flush the buffer, it just prints a newline, you might be thinking of endl.

##### Share on other sites
iMalc    2466
'while' isn't broken, it's your printf that isn't outputting anything.
Just use the keyword as you would normally instead of making unrealistic loops that don't flush the output buffer or something.

##### Share on other sites
doynax    850
Quote:
Original post by Roboguy
Quote:
 Original post by ZahlmanHave you tried it with a \n after the 'this'? It may not be getting a chance to flush the buffer to console.

\n doesn't flush the buffer, it just prints a newline, you might be thinking of endl.

A newline usually flushes FILE streams.

Interactive streams are eighter line buffered (setvbuf(stream, NULL, _IOLBF, 0)) or completely unbuffered (setvbuf(stream, NULL, _IONBF, 0)), except stderr which is guaranteed to be unbuffered.

All Unix machines I've encountered has been buffered although I suspect that Win32 uses unbuffered mode (or has special handling for the console).

Quote:
 MSDN_IOLBF For some systems, this provides line buffering. However, for Win32, the behavior is the same as _IOFBF - Full Buffering.

##### Share on other sites
I'd like to take this time to point out that, if you're using Vis Studio or any number of other quality compilers, you have a useful tool called a debugger. Use it. Going with the Vis Studio interface, that's done as follows:

- Right click on some code before the start of your loop
- Click "Run to cursor"
- The program will run, and then pause at the line you selected, returning you to your source code.
- Press F10 to step through your code, executing one line at a time.
- When you come to your loop, what happens? Does it skip over? Does the debugger come crashing down? Does the current-line cursor rotate around and around within the loop, indicating that your while is in fact working fine, and just not do anything, indicating that you have some other problem?

##### Share on other sites
fredizzimo    133
Quote:
 Original post by iMalc'while' isn't broken, it's your printf that isn't outputting anything.Just use the keyword as you would normally instead of making unrealistic loops that don't flush the output buffer or something.

How often do you see fflush statements in console programs? I can tell you, not very often, because you usually don't need to flush the output buffer manually.

Unless the internal buffer is really huge, there would be forced flushes in a loop like that. Even a MB buffer would fill up and be flushed quite fast. So its very unlikely that it's buffering that causes his problem.

And now back to the real problem

I still think the real problem is somewhere else in the function, either that it doesn't enter the loop at all, or that he is corrupting the stack somehow. Jtrask is right about using the debugger, sooner or later you have to learn to use it. A problem like this should be easilly detectable in the debugger. But if not then more source code is needed.

##### Share on other sites
smart_idiot    1298
Try fflush, if not, maybe you accidently did something silly like while(1); {...}.

When I used msys in Windows, it was really anal about flushing your buffers. It would probably hold several kilobytes of text before it would get around to finally displaying it, which usually resulted in you not seeing anything and thinking there was something horribly wrong with your program.

##### Share on other sites
fractoid    703
If it's a console app, it's almost certainly the \n issue - I'm reasonably sure that whatever the stdio streams do, the console window itself doesn't display text until it receives an end-of-line character (\n works, iirc, although endl is better).

##### Share on other sites
fredizzimo    133
Quote:
 Original post by fractoidIf it's a console app, it's almost certainly the \n issue - I'm reasonably sure that whatever the stdio streams do, the console window itself doesn't display text until it receives an end-of-line character (\n works, iirc, although endl is better).

That's not true, at least not in winxp. I just did a quick test with visual studio 2003.

[source}int main(){	while(1)	{		printf("this");	}	return 0;}

and it prints "this" forever. And I dont seem to remember that this was the case when I used vc++ 6 and other versions of windows either.