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 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 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.