0

This is a basic question but I've never really encountered this type of for loop before for(;;) .

How do you use this type of loop? Do you simply break from it by calling break;?

I think this is a simple question but I can't find a solution online for it.

Alaeddine
  • 1,571
  • 2
  • 22
  • 46
AbhishekSaha
  • 705
  • 3
  • 9
  • 24

5 Answers5

3

This is just one of the ways to run the loop infinitely. There's no initial condition, no terminating condition, the loop runs forever, unless you break it.

And if your next question arises which out of the two while(1) or for(;;) is better (performace/speed): while (1) Vs. for (;;) Is there a speed difference?

Community
  • 1
  • 1
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
2

A for(;;) loop is the same as while(true). Any compiler will compile both as the same.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Shang
  • 518
  • 3
  • 13
1

This for loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for loop can be used (with empty expressions), such as:

for (;;)
   //loop body

You can break out of it. This is similar to an infinite while loop

while(1) 
   //loop body
RDizzl3
  • 846
  • 8
  • 18
0

Yes, you break, or call a non-returning function such as exit().

bames53
  • 86,085
  • 15
  • 179
  • 244
0

The for(;;) is simply an infinite loop, Just like the While(true) loop.

and you can use the Break; to stop the loop or if you're using it in a return function.

for(;;){
  if(condition){
        break;
  }else {
    //do something
   } 
}

or

 int function(){
         for(;;){
           if(condition){
            return 0 ;
            }else {
        //do something
       }
    return 0 ; 
    }
Alaeddine
  • 1,571
  • 2
  • 22
  • 46