while loop question

R

rzed

I coded a while loop this way:
while( fgets( buff, MAXBUFF, ifp ) != NULL && rtype == 0 )
....

with the effect that buff contained every other line in the file (that is, the first, third, fifth, etc.).

Coding it this way:
while( rtype == 0 && fgets( buff, MAXBUFF, ifp ) != NULL )
....

put every line in the file in buff. 'rtype' was set to zero before the loop in each case, and within the loop it might or might not be altered, but that logic did not change.

What's going on here? Why would I get different results?
 
R

rzed

I coded a while loop this way:

while( fgets( buff, MAXBUFF, ifp ) != NULL && rtype == 0 )

...



with the effect that buff contained every other line in the file (that is, the first, third, fifth, etc.).



Coding it this way:

while( rtype == 0 && fgets( buff, MAXBUFF, ifp ) != NULL )

...



put every line in the file in buff. 'rtype' was set to zero before the loop in each case, and within the loop it might or might not be altered, but that logic did not change.



What's going on here? Why would I get different results?

Never mind. Silly me. Changing the order shortcuts the condition evaluation, of course. Slap! Slap!
 
G

glen herrmannsfeldt

rzed said:
(snip)
(snip)
Never mind. Silly me. Changing the order shortcuts the condition
evaluation, of course. Slap! Slap!

I presume there is a loop that you didn't show.

Also, you may or may not find a do{}while loop to be useful.

-- glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top