The three parts within for parentheses are:
- What to do before the loop
- A predicate, if it returns true, continue looping
- What to do after each turn through the loop
So:
for(a;b;c) {
d
}
... is equivalent to:
a;
while(b) {
d;
c;
}
... with the bonus feature, that if any of a,b,c,d are missing, it "works" b being missing is equivalent to true.
So:
int i=0;
for(;i<10;i++) {
println('hello');
}`
... is equivalent to:
int i=0;
while(i<10) {
println('hello');
i++;
}
And:
for(int i=0;;i++) {
println('hello');
}`
... is equivalent to:
int i=0;
while(true) {
println('hello');
i++;
}
... which is an infinite loop because while(true).
And:
for(int i=0;i<10;) {
println('hello');
}`
... is equivalent to:
int i=0;
while(i<10) {
println('hello');
}
(... which is an infinite loop because i never reaches 10)
And:
for(;;) { println('hello'); }
is:
while(true) { println('hello'); }
%is? Or you asking why it does not have all three things in the loop?[initialization]; [condition]; [final-expression]For loop explaination is great in the docs on MDNforloop is omitted, it's equivalent to havingtruethere. It's not all that uncommon to seefor(;;) { /* do stuff */ }when someone wants to have an infinite loop, but feels squeamish about writingwhile(1).for (;;), this code comes from the chapter explainingbreakand how this would be an infinite loop without the break.for (let current = 20; true; current++). Last but not least, onesbreakis called the loop stops.