0
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
  • 1
    Do you know what % 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 MDN Commented Nov 26, 2019 at 16:58
  • 2
    If the condition part of a for loop is omitted, it's equivalent to having true there. It's not all that uncommon to see for(;;) { /* do stuff */ } when someone wants to have an infinite loop, but feels squeamish about writing while(1). Commented Nov 26, 2019 at 16:59
  • About the for (;;), this code comes from the chapter explaining break and how this would be an infinite loop without the break. Commented Nov 26, 2019 at 17:08
  • The loop goes as follows: 20 % 7 results 6, because it doesn't equal 0 it returns false and the if doesn't trigger. Than the loops continue so 21 % 7 result 0, because it does equal 0 it retuns true and the if triggers. As far the for condition itself. The default condition of a for loop is true. So it basicly reads: for (let current = 20; true; current++). Last but not least, ones break is called the loop stops. Commented Nov 26, 2019 at 17:10

4 Answers 4

3

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;
Sign up to request clarification or add additional context in comments.

3 Comments

This is a very contrived example. Nonetheless, since the "condition test" is entirely omitted from the for statement, the loop would ordinarily run forever. And, it would run forever if the break statement were not eventually executed.
But in the particular example it will iterate 2 times and thats it, right?
@Animus Yes, but it would only hit the print once.
1

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

0

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

0

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'); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.