[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gcc: A while Loop Always Skips its first Statement.



On 08/20/2010 08:01 AM, Martin McCormick wrote:
I have written some C code that appeared to be good until I put
a while statement in to one of the modules and then all went to
at least Purgatory. It all compiles beautifully but I was
alerted to something terribly wrong when the program began
misbehaving. I can't even say that it is flaky because it does
exactly the same wrong things every time without fail.

	There is a variable I call NEXTSYS which is boolean and
my while statement now looks like:

while (NEXTSYS) { /*code to execute if NEXTSYS does not = 0*/
C statements (It doesn't seem to matter what statements);
} /*code to execute if NEXTSYS does not = 0*/

	What happens every time is that gdb traces perfectly up
to the while statement, skips it, and executes the statements
inside the braces. After that, it is in the while loop and
execution looks normal but the damage is already done. I have
also seen it trace the wile statement and then skip the next
executable statement in the loop no matter what it is.

	Reading about the while loop tells me that the statement
after while must be true to execute. If I put a boolean variable
inside the statement as in (NEXTSYS) does that make a true
statement if it is not 0 and false if it is?


What happens when you Google "C true false" or write a test program that does:

while ( 1 == 1 )
{
    blah
}

while ( 1 )
{
    blah
}

while ( 0 )
{
    blah
}


--
Seek truth from facts.


Reply to: