c++ for loop question

hollowman

Diamond Member
Feb 19, 2001
4,864
0
76
in c++, what does for ( ; ; ) do?

I thought for loop at least require a conditional statement?

example of the code would be...


//code starts here

void Huffman::buildDecodingTree(ifstream & codeIn)
{
char ch; // a character
string code; // its code
for ( ; ; )
{
codeIn >> ch >> code;

if ( codeIn.eof() ) return;
insert(ch, code);
}
}

so what exactly is for ( ; ; ) doing in this snippet of code?
thank you for your help.
 

hollowman

Diamond Member
Feb 19, 2001
4,864
0
76
Originally posted by: xtknight
As far as I know, it's an infinite loop.

hmm.. so then i guess when it is used with inputFileStream, it will halt when there is no more information to read in?

because i have ran the program above and it does halt after reading in the file completely.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Yup...

if ( codeIn.eof() ) return;

This breaks it out of the infinite loop (actually out of the function).
 

hollowman

Diamond Member
Feb 19, 2001
4,864
0
76
Originally posted by: xtknight
Yup...

if ( codeIn.eof() ) return;

This breaks it out of the infinite loop (actually out of the function).

ah... i see.

thanks =)