1

I am writing a program that involves reading numerous bits of data from files, and I have ran into a bit of a dilemma (being a relatively inexperienced programmer). If a while loop is contained within another while loop, which conditions need to be fulfilled in order to leave the loop? My specific situation is shown below, (emptyLineFound is a Boolean set to be true when empty line has been found)

while(!file.eof()) {
     ....
     while(!emptyLineFound) {
                ....
     }
}

Which condition takes priority? Must they both be satisfied to leave that section of code? e.g if it is at the end of the file and an empty line can not be found as there is no line, will it mess up?

Matt Lee
  • 11
  • 1

5 Answers5

3

Both sections do not have to be satisfied for the nested while to exit.

 while(!file.eof()) { 

Will continue to run while the file is not at the end of the stream.

 while(!emptyLineFound) { .... } 

Will continue to run while an empty line is not found.

Once an empty line is found, you will drop out of the nested loop and the parent loop will continue until the condition is satisfied (EOF is reached).

Darren
  • 63,390
  • 21
  • 126
  • 134
  • Thanks for the reply. I understand what you're saying, when an empty line has been found, it will run past that for the rest of the parent loop. But, it runs into the same empty line while loop very shortly after, so back in the same situation. Probably worth noting that my end of line check is just checking to see if getline=="", so I suspect that when there is no more data at the end of the file, the empty line loop can not be left before the parent end of file loop. Sound plausible? – Matt Lee May 02 '13 at 07:43
1
While(ExitCondition)
 {
    // you are at this point if ExitCondition fails .

    while(another-ExitCondition)
      {

         //  you are at this point if another-ExitCondition fails


       }

     // you will come at this point once another-ExitCondition becomes true
  }

    // you will be at this point once  ExitCondition becomes true .
birubisht
  • 832
  • 7
  • 16
0

In your question, the inner loop will end when emptyLineFound is true. The outer loop will continue until file.eof() is true. Presumably something in the inner loop sets emptyLineFound to false before the next loop.

Note that the conditions are tested as the code gets there, so, in theory you could have something like this:

while(!file.eof())
{
   while(!emptyLineFound) 
   {
       ... do some reading ... 
       if (file.eof())
       {
          ... reset file so we can do more reading ... 
       }
   }
}

and the loop would continue forever, except if file.eof() is true from the very beginning of the loop.

By the way, in general, using !file.eof() as a loop control is a pretty poor idea. It is much better to use the read operation itself, e.g. while (file >> var) ... - since while (!file.eof()) tends to lead to an extra iteration, because file.eof() isn't true until you have read PAST the end.

Mats Petersson
  • 119,687
  • 13
  • 121
  • 204
  • Thanks for the speedy replies everyone. I think I understand it better now and now think my problem is slightly different. My program reads in student names, courses, marks etc., with each students record separated by a blank line. My code so far reads in all the data fine, no matter how many records or courses, up until the last line of the last record, which gets read in infinitely. First, what will using file.ignore(numeric_limits::max(), '\n') do if it's just read the last line of the file? – Matt Lee May 02 '13 at 07:52
  • You have probably got a different problem. Any chance of you actually posting the WHOLE code (you may want to strip it down so that it's only showing the "faulty" part, but a complete program is much better than something that is "these are a few lines of my code" - typically, when doing this, you often find the cause of the problem too!) – Mats Petersson May 02 '13 at 08:03
  • Unfortunately I am unable to as I have no access to wifi and am posting on here with my phone. However, if I still have problems tomorrow I will post then. Thank you for your help – Matt Lee May 02 '13 at 08:12
0

Each time the outer loop is repeated, the inner loops are reentered and start new.

Mike U
  • 79
  • 1
  • 9
0

Perhaps the code becomes clear if expressed differently:

void f()
{
    // ...
    while (!file.eof()) {
        // ...
    }
}

int main()
{
    // ...
    while (!emptyLineFound) {
        f();
    }
}

This is semantically the same as your program; it has the same overall logic.

From here, you can clearly see that a call to f() will not terminate until f.eof() is true, and that main will repeatedly call f() (and thus perform the "inner" loop) until emptyLineFound is true.

Kaz Dragon
  • 6,266
  • 2
  • 32
  • 44