How would I loop this? (newbie question)

Started by
12 comments, last by raz0r 18 years, 1 month ago
What exactly does that for(;;) mean? I've never seen it used that way with the ';;'. Would that be different than for()?

Thanks for the additional help :)
Advertisement
for(;;) is equivalent to while(true), that is it's an infinite loop. You can leave any of the three for fields blank if you don't need them (the second field defaults to true), e.g.:

int g_iLoopCt = 0;for (; g_iLoopCt < 8; ++g_iLoopCt){  /// ...blah}
for(;;) repeats all functions inside its '{' '}' brackets again and again until broken. Like while() only it repeats without stop unless it reaches a 'break;'command. for instance:

for(;;){    cout << "Text";}

The above would repeat 'Text' 'Text' 'Text' endlessly.

And the below:

for(;;){    cout << "Input number: ";    cin >> int num;    if(num == 7)    {        break;    }}

Would keep asking for a number until a seven is entered, at which point it will 'break' and cut through the loops.

I am sure there are better ways to do things, but I am still learning c++ myself.
Quote:
for(;;)
{
cout << "Input number: ";
cin >> int num;
if(num == 7)
{
break;
}
}


Like socrates200X stated:
int iDummy = int();for( ; iDummy != 7 ; ){	std::cout << "Input Number: ";	std::cin >> iDummy;}

This topic is closed to new replies.

Advertisement