for (let current = 20; ; current = current + 1) {
if (current % 7 == 0) {
console.log(current);
break;
}
}
// → 21
How does this work syntax wise i did not understand it. Why does it print 21? I'm reading eloquent js and stumbled upon this.
4 Answers 4
Current starts at 20.
let current = 20
It is incremented by 1
current = current + 1
Once it is divisible by 7 (0 means no remainder so it is divisible)
if (current % 7 == 0)
Print current(21) -> 21 % 7 = 0 and stop
console.log(current);
break;
3 Comments
for statement, the loop would ordinarily run forever. And, it would run forever if the break statement were not eventually executed.First you have to know what the % sign means.
% is used for modulus - It means remainder.
For Example:-
5%2 --- // (5/2) here remainder is 1 so result is 1.
Now move to the actual question:--
LOOP
for (let current = 20; ; current = current + 1) {
if (current % 7 == 0) {
console.log(current);
break;
}
}
In first Turn
1) current value is 20
2) if(current % 7 == 0) -- Define Condition, the value of remainder when "current" is divided by 7 (current/7) Here this will resolve as
a) (20%7 = 6 (remainder))
b) if(6 == 0) -- false
3) Skip the if block
4) Now, current = current+1 // current = 21
In Second Turn
1) current value is 21
2) if(current % 7 == 0)
Resolve as ---
a) (21%7 = 0 (remainder))
b) if(0 == 0) -- true
3) Goes into if block
4) Print the value of current on console, i.e., 21
5) execute break statement and terminate the loop
--- End Program ---
Comments
The empty statement (i.e. just a semi colon) is a valid statement in javascript. This just means that there is no condition being checked at the beginning of each loop iteration in that for loop.
Now, if we look at what this loop does, we can see that on the first iteration, current = 20 and hence the inner condition fails (since 20 is not divisible by 7).
The next iteration occurs, current is incremented to 21, and now the inner condition passes (21 is divisible by 7). Hence, we print 21 and break out of the loop.
Comments
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.