For(;;)

Jan 24, 2013 at 3:26pm
What does the double semi-colon do in the following piece of code:

for(;;)
{
int nValue = 0;
std::cout << "Enter number: ";
std::cin >> nValue;
Jan 24, 2013 at 3:34pm
That would make it loop infinitely. (There is no condition, no code to run on each iteration, and no initial statement.)

You're missing a bracket though.
Last edited on Jan 24, 2013 at 3:34pm
Jan 24, 2013 at 3:34pm
It's an empty for loop. It behaves like this while loop, which repeats forever (an infinite loop):
1
2
3
4
    while (true)
    {
        // repeat forever
    }


See tutorial about the various loop structures.
http://www.cplusplus.com/doc/tutorial/control/
Jan 24, 2013 at 3:34pm
Usually a for loop will look something like this for (int i = 0; i < 10; ++i)

for (int i = 0; i < 10; ++i)
The first thing is something that will run once when the loop starts. It is often used to initialize some variable, in this case i.

for (int i = 0; i < 10; ++i)
The middle part is the condition. Before each iteration it will check the condition and if it is false the loop will stop.

for (int i = 0; i < 10; ++i)
The last thing is something that will run after each iteration.

You don't need to have any of these. If the condition is missing the loop will run infinitely. To make it stop you would have to use break, return or something like that.
Last edited on Jan 24, 2013 at 3:35pm
Jan 24, 2013 at 3:54pm
Thank's Magtheridon96, I copied part of a program.
Topic archived. No new replies allowed.