i broke while

Started by
34 comments, last by Tron3k 19 years, 6 months ago
for some reason, my while loop no longer works, no matter what code i do, but dowhile still works. any insight?
| Member of UBAAG (Unban aftermath Association of Gamedev)
Advertisement
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.
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.
| Member of UBAAG (Unban aftermath Association of Gamedev)
Quote:Original post by Cibressus
for some reason, my while loop no longer works, no matter what code i do, but dowhile still works. any insight?

We need more information, preferably source code, to be able to help you.
There's not much to say about do { } while and while except that they perform two different functions.

edit: 23 seconds too late =)
Cibressus, are you serious?
Quote:Original post by Cibressus
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.


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.
Quote:Original post by Cibressus
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.

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?
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.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Is it possible to overload "while"? *ponder*

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Quote:Original post by felisandria
Is 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.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement