简体   繁体   中英

Use of “for (;;)” in a C# application?

I've been looking through some sample source code for an application I use and I came across this line:

for (;;)
{
// The rest of the application's code
}

It looks like this is to create an infinite loop, but I'm not familiar with ";;" and it's very hard to Google unfortunately.

Yes, that is an infinite loop. It's an ordinary for loop with no condition expression.

From the documentation for for :

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

 for (; ; ) { // ... } 

it is infinite loop.

equal to

while (true) {
}

I just want to clarify :

;; is not a special operator or something - it's a regular for loop.

regular for loop looks like that:

for (do_before_loop**;** finish_loop_when_this_condition_is_false**;** do_after_each_iteration);

if you leave all 3 parts empty you get ;; - and since you don't have an exit condition - this is an infinite loop.

Normally You write Your loop like this:

for (int i = 0; i < 10; i++)
{
// The rest of the application's code
}

Now, when You want Your loop to be infinite one, You just have to remove the "int i = 0", condition "i < 10", and incrementation "i++". If You do this, then in a for statement You will see only ";;"

for (;;)
{
// The rest of the application's code
}

Yes, it's an infinite loop.

All parameters in the for statement are optional, and the condition defaults to true , so it's the same as:

for (;true;)

or:

while (true)

This is the same as for (<initial>; <condition>; <increment>) , you're simply leaving out initial, condition and increment. In this case, condition will always be considered true.

It is an ordinary use of the infinite loop. We use this loop in case we don't know how many times we need to execute the code inside the loop. Therefore we need to define also a condition to get out of this loop. Here is an example of receiving a long message from a server :

for (;;)
{
     //Receive the maximum allowed size of the message
     //Save(Concatenate) the received data in a variable (TotalReceivedData)
     // If the length of the received part of the message is equal to zero , break the operation
     //<Break the loop>if(message.length == 0){break;}</Break the loop> 
} 

And here you can use your full received message stored in the variable TotalReceivedData.

I added this example because I just came across a case like this.

Interesting reading in this article:

http://csharpdevelop.blogspot.com/2004/05/writing-infinite-loop.html

This kind of code occurs quite a bit in worker threads waiting for some work to do. It is a common code pattern. All code in the looping statements body is critical. The "quit condition" must be checked frequently. This implies that the work to do must be short. Usually this is one smaller chuck of work out of the larger set of work it was given to accomplish.

Its the first entry in google if you are interested I googled C# for ;; And got this link http://msdn.microsoft.com/en-us/library/ch45axte.aspx Click C# and msdn states:

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

for (; ; ) { // ... }

A normal for loop has these elements

 for ( for-initializer ; for-condition ; for-iterator ) embedded-statement

eg

for(int i = 0 ; i < 10 ; i++) {
   foo();
}

Any of those elements can be omitted, and you're left with for(;;) , which is an infinite loop.

The c# language specification specifically states

If the for-condition is omitted from the for statement, then evaluation of definite assignment proceeds as if for-condition were replaced with true in the above expansion.

So, for(;;) is the same as for(;true;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM