What does (?;) Mean?

I am confused by the for(;;) construct. I think this is a form of unlimited loop reduction, but I cannot be sure.

Here is the code:

 for(;;) { //whatever statements } 
+4
source share
6 answers

Your hunch is right; it is an infinite loop. * This is a common C-idiom, although many people (including me) find the following less cryptic:

 while (1) { whatever statements; } 


* This is infinite if there is no break / return / etc. inside the body of the cycle.
+12
source

This is a closed loop. Sometimes this is written using while :

 while (1) 

or even better:

 while (true) 

I would expect to see a break or return inside any such loop, regardless of whether it is written with for or while . There must be some kind of abnormal flow of control, or it really will be an infinite loop.

+2
source

Yes, that for C syntax with empty fields for initialization expression, loop condition, and increment expression.

The for statement can also use more than one value, for example, this pattern:

 for (i=0, j=100, k=1000; j < 500 || i<50 || k==5000; i++, j+=2, k*=6) {}; 

Maybe one step beyond understanding for ? =)

+2
source

Yes, expressions in a for loop are optional. if you omit them, you get an infinite loop. Way out - a break or exit.

+1
source

This operator is basically equal to:

 while(1) {} 

There is no initial, no condition and step instruction.

0
source

As I understand it, for (;;) creates an intentional loop without exiting. It is expected that your code will exit the loop based on one or more conditions. This was once provided to me as a cleaner way to do it, until a false loop, which was not considered good syntax. Based on the exit condition, it is easier to send a function, for example, to process a result, failure, warning, or success.

My explanation may not be the reason that someone used this construct, but I will explain in more detail what this means to me. This design may be a β€œClean C” way to have a loop in which you can follow several steps in succession, whose completion means something like your application, completed all the initialization steps.

 #define GEN_FAILURE -99 #define SUCCESS 0 /* perform_init_step1() and perform_init_step2() are dummy place-holder functions that provide a complete example. You could at least have one of them return non-zero for testing. */ int perform_init_step1(); int perform_init_step2(); int perform_init_step1() { return 0; } int perform_init_step2() { return 0; } int ret_code = GEN_FAILURE; for(;;) { if(SUCCESS != perform_init_step1()) { ret_code = -1; break; } if(SUCCESS != perform_init_step2()) { ret_code = -2; break; } break; } 

If part of the initialization fails, the loop collapses with a specific error code.

I came to use C by doing a lot of firmware, writing in assembler. Good assembler programmers taught me to have a single entry point and one output. I took their advice to heart, because their creed helped them a lot when debugging.

Personally, I never liked the for (;;) construct, because you can have an infinite loop if you forget to break; in the end.

Someone I worked with came up with do..until (FALSE), but the amount of C furvor due to this should not be counted.

 #define GEN_FAILURE -99 #define SUCCESS 0 /* perform_init_step1() and perform_init_step2() are dummy place-holder functions that provide a complete example. You could at least have one of them return non-zero for testing. */ int perform_init_step1(); int perform_init_step2(); int perform_init_step1() { return 0; } int perform_init_step2() { return 0; } int ret_code = GEN_FAILURE; do { if(SUCCESS != perform_init_step1()) { ret_code = -1; break; } if(SUCCESS != perform_init_step2()) { ret_code = -2; break; } } until (FALSE); 

This is done once, no matter what.

0
source

Source: https://habr.com/ru/post/1383287/


All Articles