0

According to MDN, do while loop states that this is the syntax and do runs once despite the condition

do statement while (condition);

The following is my code

let mergeArr3 = (arr1 , arr2) => {
   let i = 1;
   do {
      console.log('hello') //prints hello 5 times
      i++;
   }
   while (i < 6 ) {
      console.log(i) //prints 6
      console.log('world') // prints world once
   }
   
}

mergeArr3(arr1 , arr2)

I am struggling to understand two things:

  1. Why does console.log(i) print 6 when 6 < 6 suppose to be evaluated to false and it shouldn't be running that line of code when i === 6.

  2. Do is suppose to run once, but why is it running 5 times?

2
  • a: Because its while less than 6, so when it's 6 it ends. b: because [1,2,3,4,5], yes there is 5 times here.. The interesting thing that you might have accidentally stumbled upon, is why do loops in computing code often start at 0,.. because < 6 starting at 0, is 6 iterations. If you want 6 iterations but still start at 1, use <= 6. Commented Oct 4, 2020 at 14:39
  • The do loop runs "at least once", not "exactly once". It would also be clearer to you what's going on if you inserted semi-colons at the end of your lines of code. There should be one as follows: while (i < 6 ); {, for example, and the open brace is part of the next block of code. Commented Oct 4, 2020 at 14:41

2 Answers 2

4

the "loop" part is only the block between do and while.

It's executed at least once, and until the while condition is falsy

The block you wrote after the while is just an independent block of code, that should be read like this for better clarity :

   let i = 1;

   // loop do -- while
   do {
      console.log('hello') //prints hello 5 times
      i++;
   }
   while (i < 6) // if the condition here is true, execute the above block again.

   // this is just a legal block of code but completely unrelated to the loop.
   {
      console.log(i) //prints 6
      console.log('world') // prints world once
   }

So, the last block after the loop is executed when i is not less than 6, i.e. when i is exactly 6. This explains the output.

Sign up to request clarification or add additional context in comments.

Comments

0
 while (i < 6 )
       {
          console.log(i) //prints 6
          console.log('world') // prints world once
       }

The part after while condition is never the part of do-while its just normal block with console inside. This will always be interpretes like below by JS or any language that supports do-while:

let mergeArr3 = (arr1 , arr2) => {
   let i = 1;
   do {
      console.log('hello') //prints hello 5 times
      i++;
   }
   while (i < 6 );
       {
          console.log(i) //prints 6
          console.log('world') // prints world once
       }
       
    }

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.