What does the ;; Operator in C/C++ for(;;) do Found in toggle.cc in the GCC Demos
nwdomain
Posts: 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) {
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);
}
} OUTA ^= mask;
waitcnt(freq+CNT);
}
Comments
EDIT...
I should say that ";;" is not an operator, but a part of a for-loop structure
Typical for loop example:
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
Where the three statements in the "for" are:
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)".
A clarification: the ;; is not an operator, but just two semicolons. The general form of a for statement has 3 parts, separated by semicolons: 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: 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 is an infinite loop that calls do_something().
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.
Good call! How about this?
A for ( ; 0 ; ) also works and has a muggy character to it ^_^ .