while loop question

Discussion in 'C Programming' started by rzed, Oct 17, 2013.

  1. rzed

    rzed Guest

    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?
    rzed, Oct 17, 2013
    1. Advertisements

  2. rzed

    rzed Guest

    Never mind. Silly me. Changing the order shortcuts the condition evaluation, of course. Slap! Slap!
    rzed, Oct 17, 2013
    1. Advertisements

  3. 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
    glen herrmannsfeldt, Oct 17, 2013
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.