Sunday, February 16, 2020

while and do-while In JavaScript

while and do-while In JavaScript

while loop

The "while" is a common iteration statement used in most of the programming languages. The "while" loop is an iteration loop like for-loop and do-while. When we need to execute a block of code several times repeatedly, we can use iteration statements. "while" statement is useful when the number of iterations is not known, and we need to execute the iteration on the basis of a pre-specified condition. 

The syntax for the while-loop is

the block of statements will keep executing until the condition fails.

while-loop in JavaScript

"do-while" loop

The "do-while" loop is another version of the "while" loop. The "do-while" is executed at least once due to the do statement that appears before the while statement. It means that the control of execution will at least once enter into the "do-block".
The syntax of the "do-while" is 


A semicolon (;) is required after the end of "do-while" statements.  We also need to use some update statement within the "while" and the "do-while" statements, so that condition within while parenthesis is updated, and at some point in time loop will come to halt the execution, otherwise, they can turn into infinite loops.

do while in JavaScript