What does such a design mean?

for(;;) { //операторы } 

It is clear that the for loop, but what about its parameters?

  • 6
    eternal cycle? :-) - gecube 1:54 pm
  • Why parameters? any for loop can always be expanded in a while. See it. for (A; B; C) {D;} <=> A; while (B) {D;C;} A; while (B) {D;C;} . Any of the elements can be skipped. If we skip B, then we take it as true. Well, we can always write not a block {...} , but c ; : while (1); // вечный цикл while (1); // вечный цикл - gecube 2:07 pm
  • By and large, it is possible to forge for (A; B; C, D) - Specter
  • But this is just an extension of the idea of ​​using the operator,. Indeed, it is indeed possible then for (A1, A2; B; C1, C2) . - gecube
  • @STIZZ, it is so masked {Label :; ... goto Label; } but, there is a deep sense in it. At first glance it is clear (in the case of for (;;) {}) that from the outside of the for (;;) {} block there are no transitions to its beginning (otherwise it would be necessary to search for all goto Label;). - avp

3 answers 3

This is an infinite loop (equivalent to while(true) {} ). Sometimes this is needed. Exit from it is usually by break , or by return .

  • Often also while (1) {...} - BuilderC pm
  • @BuilderC, in fact, true is equivalent to 1, and False is 0. In the computer memory, by the way, they are represented as numbers. Therefore, since there is nothing to add, it is better not to blurt out anything ... - AseN
  • 3
    It is a pity that it is impossible to minus. > true is equivalent to 1 Aha, this is taking into account the fact that, say, while (255){...} will behave the same way :-) - gecube
  • everything is simple: false = 0; true =! false; - Specter

but what about its parameters?

they are trivially omitted, therefore nothing happens at the beginning of the cycle, there are no checks for the end of the cycle, and there is no block describing the action, hence the cycle will work equivalently to the construction


    I also add that in the for loop and the body is optional. Therefore, there is also a reverse situation: the parameters are there, but the bodies are not. Because all the necessary operations are already in the parameters.

    You will be surprised even more, but sometimes even such a construction makes sense:

     do { make_me_happy(); } while(0) 
    • 2
      I will add: sometimes it is useful even without a body, and without parameters - for (;;); - paulgri
    • What for? Looping the main program to keep its threads alive? - VadimTukaev
    • one
      @VadimTukaev, meaningless. Than to waste processor time, it is better to ask the OS to “freeze” the main thread before completing the additional threads. - ߊߚߤߘ