for(;;) and while (true) loops

I was looking around in the forum and I saw some people using for(;;)
Isn't it the same thing as while (true)? What's the difference between them?
closed account (Dy7SLyTq)
for(;;) takes two less characters. i dont like it though, because in practice for (well the traditional for) is used for a set number of times. while is used typically for an unknown amount of cycles. so while it wont make a difference, its a matter of choice
I use for( ; ; ) in preference to while(true) as I build using Visual C++ at /W4 (warning level 4) and want to avoid

warning C4127: conditional expression is constant

I could use Visual C++ pragma to disable the warning at this point, I suppose, but I would never disable this warning globally in case I missed a real problem.

I happen to prefer

1
2
3
4
5
6
7
    for( ; ; )
    {
        do_stuff(stuff);
        done = check_stuff(stuff);
        if(done)
            break;
    }


to

1
2
3
4
5
6
7
8
9
10
11
#pragma warning(push)
// warning C4127: conditional expression is constant
#pragma warning(disable: 4127)
    while(true)
    {
        do_stuff(stuff);
        done = check_stuff(stuff);
        if(done)
            break;
    }
#pragma warning(pop) 


Andy

PS There's also this

1
2
3
4
5
6
7
8
9
    // (void*), to disable warning:
    // warning C4127: conditional expression is constant
    while((void*)0, true)
    {
        do_stuff(stuff);
        done = check_stuff(stuff);
        if(done)
            break;
    }


which is also worse than for( ; ; ) to me.
Last edited on
1
2
3
4
5
6
int score = 10;
for (; score < 10;)
{
++score;
}
cout<<score<<endl;

1
2
3
4
5
6
7
8
while ( true )
{
++score;
if ( score >= 10 )
{
break;
}
}


You can use an empty statements in creating your for loops, as i did in the following loop:
for ( ; score < 10 ; )
i used an empty statement for the initialization and action statements. That's fine because i declared and initialized score before the loop and incremented it inside the loop body. Different programmers have different traditions, some programmers prefer to create infinite loops using a for statement that begins for(;;). Because the test expression in this loop is the empty statement, the loop will continue until it encounters some exit statement such as break;
Uk Marine.
closed account (Dy7SLyTq)
@andy: why does it give you a warning? gcc doesnt, and i prefer while(true)
DTSCode wrote:
why does it give you a warning? gcc doesnt, and i prefer while(true)

That's a question for the Microsoft compiler team.

Andy

C/C++: How to use the do-while(0); construct without compiler warnings like C4127?
http://stackoverflow.com/questions/1946445/c-c-how-to-use-the-do-while0-construct-without-compiler-warnings-like-c412
Last edited on
Okay thanks for the answers!
closed account (S6k9GNh0)
Once upon a time for(;;) { } was an optimization over while(true) { } since the compiler wouldn't optimize out the boolean check (even though its pointless) and some other internals that confused the nature of the compiler.

Now adays, it makes absolutely no difference (unless you turn off all optimizations... which I believe someone did before on this forum and posted the assembly output to show the difference... can't seem to find it though).
Last edited on
Difference between for and While:
For Loop:
for..loop statements are used to perform certain task for more than one times.
It is an entry controlled loop.
The for statement, first part initializes the loop. Initilization is executed only once. Here one or more variables can be initialized.
The Second part tests the condition, if result of the condition is true then it enters the loop part, it comes out of loop when result of the condition comes out to be false.
The third part is used for increment or decrement. More then one expressions can be used in increment/decrement.

While Loop:
When a loop is described with a while statement , you can only specify the looping condition.
It is an entry controlled loop.
If the result of loop condition is true then the loop block is executed.
When the result of loop condition become false, it executes next statements, written after block scope.

Read more with Example:
http://cbtsam.com/cppl1/cbtsam-cppl1-054.php
Topic archived. No new replies allowed.