While loop question.

This is a discussion on While loop question. within the C++ Programming forums, part of the General Programming Boards category; Hi, Okay, the following loop has been taken from a book I am studying from: Code: while(true) { Month m; ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    While loop question.

    Hi,

    Okay, the following loop has been taken from a book I am studying from:

    Code:
    while(true) {
    Month m;
    if(!(is >> m)) break;
    y.month[m.month] = m;
    }
    My first question is regarding the condition while(true), does this basically mean that the loop qill continue until something explicitly states to exit the loop? In this case, the break statement? Or this could be triggered by returning something other than 1? Is that right?

    Also, in this example, the >> operator has been overloaded to read from an istream, in this case a txt file. If there is nothing read then this will break the loop, however the statement below that does not appear to do anything with the input stream? There is nothing explicit within that loop to say read from a stream, yet I think thats what it implies? Any suggestions?

    Thanks,

    Darren.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    While forever:
    Create a month m.
    Extract a month from the stream and store it in m. If this fails, break the loop.
    Set y.month[m.month] to m.
    Loop.

    Clearer?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by darren78 View Post
    My first question is regarding the condition while(true), does this basically mean that the loop qill continue until something explicitly states to exit the loop? In this case, the break statement?
    Yes, exactly.

    Or this could be triggered by returning something other than 1? Is that right?
    Nope. The only way out of such a loop is break, return, or exit (or a call to function which calls exit).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    While forever:
    Create a month m.
    Extract a month from the stream and store it in m. If this fails, break the loop.
    Set y.month[m.month] to m.
    Loop.

    Clearer?
    A little. I can see that if there is no stream, then the loop will break. However I can't see where it says to extract a month from the stream. Does that part basically mean, get the data from the stream and if it fails, break?

    Thanks.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by MK27 View Post
    Yes, exactly.



    Nope. The only way out of such a loop is break, return, or exit (or a call to function which calls exit).
    Okay thats clear now. It's because the while loop is not a function and obviously nothing is expected to be returned.

    Thanks.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Presumably [] is overloaded here too, otherwise this line is hard to understand:
    Code:
    y.month[m.month] = m;
    I haven't seen the declarations for these or their class definitions so it would be hard to say precisely how that works out.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by MK27 View Post
    Presumably [] is overloaded here too, otherwise this line is hard to understand:
    Code:
    y.month[m.month] = m;
    I haven't seen the declarations for these or their class definitions so it would be hard to say precisely how that works out.
    No [] isn't overloaded. It is quite hard to explain, basically its classes and objects that contain a few vectors.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by darren78 View Post
    ...Does that part basically mean, get the data from the stream and if it fails, break?

    Thanks.
    That is exactly what it does. Take this code:
    Code:
    if(!(is >> m)) break;
    Now let's break it into separate lines:
    Code:
    istream& file = is >> m; // The >> operator returns the stream itself, is.
    if (!file) break; // If we were unable to extract the data from the file into m, the file will be marked as "bad".
    This is also essentially the same as
    Code:
    is >> m;
    if (!is) break;
    The is >> m extracts the month from the file, if there is one. We know that because m is a month.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    That is exactly what it does. Take this code:
    Code:
    if(!(is >> m)) break;
    Now let's break it into separate lines:
    Code:
    istream& file = is >> m; // The >> operator returns the stream itself, is.
    if (!file) break; // If we were unable to extract the data from the file into m, the file will be marked as "bad".
    This is also essentially the same as
    Code:
    is >> m;
    if (!is) break;
    The is >> m extracts the month from the file, if there is one. We know that because m is a month.
    Thanks, It's all a little clearer now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb loop question
    By filio623 in forum C++ Programming
    Replies: 10
    Last Post: 09-18-2009, 02:54 PM
  2. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  3. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  4. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  5. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21