Shop OBEX P1 Docs P2 Docs Learn Events
What does the ;; Operator in C/C++ for(;;) do Found in toggle.cc in the GCC Demos — Parallax Forums

What does the ;; Operator in C/C++ for(;;) do Found in toggle.cc in the GCC Demos

nwdomainnwdomain Posts: 1
edited 2013-06-05 13:44 in Propeller 1
/*
What does the ;; Operator in C/C++ in the for statement: for( ;; ) do This is found in toggle.cc in the GCC Demos SimpleIDE
*/

// the run loop
void run(void) {
for(;; ){
OUTA ^= mask;
waitcnt(freq+CNT);
}
}

Comments

  • dgatelydgately Posts: 1,630
    edited 2013-06-05 09:58
    It creates an "infinite loop", much like while (1)

    EDIT...

    I should say that ";;" is not an operator, but a part of a for-loop structure


    Typical for loop example:
    for (i = 0; i < 5; i++) {
    
        // do something here...
    }
    

    this will execute the code between the braces 4 times as i receives the values (0,1,2,4). It exits when expression i < 5 is no longer true. In the for ;; example, the code continues to execute as the blank expression between the ;;'s keeps the same value (basically a non-expression). So, the loop runs forever.

    dgately
  • Heater.Heater. Posts: 21,230
    edited 2013-06-05 10:07
    ";;" is not an operator. The for construct takes three statements between its round brackets, the statements are separated by a single ";" just as is used to end any normal statement. For example:
    for (a = 1; a >= 100; a++)
    {
             do_something(a);
    }
    

    Where the three statements in the "for" are:
    a = 1;       // Initializes variable a before starting the loop. Evaluated only onece on entering the loop.
    a >= 100;  // A conditional that causes the loop to exit if false. It is evaluated just before starting the loop block again
    a++;        // Evaluated at the end of each iteration, before the conditional
    
    So in this case we get to do_something with "a" having values from 1 to 99.

    But what if you miss out the three statements and just have "( ; ; )"?
    Well as stated above you have an endless loop. Same as "while(1)".
  • ersmithersmith Posts: 6,054
    edited 2013-06-05 10:10
    (Edit: It looks like dgately and Heater have already beat me to this!)
    nwdomain wrote: »
    /*
    What does the ;; Operator in C/C++ in the for statement: for( ;; ) do This is found in toggle.cc in the GCC Demos SimpleIDE

    A clarification: the ;; is not an operator, but just two semicolons. The general form of a for statement has 3 parts, separated by semicolons:
    for (initialization_statement; loop_test; loop_update)
    
    The initialization statement is run once the first time the loop is encountered; the loop test is run every time through the loop to see if the loop should be continued; and the loop_update is run on every iteration after the first. So for example to count from 0 to 9 we could do:
    for (i = 1; i <= 10; i++) {
      printf("%d\n", i);
    }
    
    It's possible to omit any of the parts of the for. If the initialization or loop update is left off, nothing extra is done the first or subsequent times through the loop. If the loop test is left off, the loop continues forever (or until an explicit break command is executed inside the loop).

    So
    for(;;) { do_something(); }
    
    is an infinite loop that calls do_something().
  • jazzedjazzed Posts: 11,803
    edited 2013-06-05 10:56
    The statement for( ;; ) can be used in code because it often compiles to smaller code than using while(1).
    Also to get 4 iterations with int n, for(n = 4; n >0; n--) can be smaller and faster than for(n = 0; n < 4; n++).

    I haven't really noticed if either is faster or smaller compiled with propeller-gcc.
  • JackBakJackBak Posts: 45
    edited 2013-06-05 12:23
    It is the same as while(1) which is a nice construct because with one single edit you comment out a complete section of looping code i.e. while(0)
  • jazzedjazzed Posts: 11,803
    edited 2013-06-05 13:44
    JackBak wrote: »
    It is the same as while(1) which is a nice construct because with one single edit you comment out a complete section of looping code i.e. while(0)

    Good call! How about this?
    /* Lower case L in l_l3 */
    #define l_l3 (1)
    
    while(l_l3) {
      puts("More Coffee.");
    }
    


    A for ( ; 0 ; ) also works and has a muggy character to it ^_^ .
Sign In or Register to comment.