0

I am trying to read data from an ifstream. I am using code as a reference that my prof gave me. I am confused as to what the for(;;) is doing and when the loop ends. I understand for loop basics but I have never seen this notation before.

//------------------------------- buildTree ----------------------------------
// To build a tree, read strings from a line, terminating when "$$" is
// encountered. Since there is some work to do before the actual insert that is
// specific to the client problem, it's best that building a tree is not a 
// member function. It's a global function. 

void buildTree(BinTree& T, ifstream& infile) {
    string s;

    for (;;) {
        infile >> s;
        cout << s << ' ';
        if (s == "$$") break;                // at end of one line
        if (infile.eof()) break;             // no more lines of data
        NodeData* ptr = new NodeData(s);     // NodeData constructor takes string
        // would do a setData if there were more than a string

        bool success = T.insert(ptr);
        if (!success)
            delete ptr;                       // duplicate case, not inserted 
    }
}

Jon Walzer
  • 65
  • 1
  • 6

1 Answers1

1

a for has 3 parts:

  1. do befor the loop starts (most of the time this is int i = 0)
  2. condition if the loop runs
  3. parts executed after 1 run through the loop

a for(;;) means it doesn't have a condition when to stop so it's endless

Simon Danninger
  • 458
  • 2
  • 7