An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
July 16, 2020 07:01 pm GMT
Original Link: https://dev.to/bhagatparwinder/javascript-do-while-loop-47il
JavaScript: Do While Loop
We learned about while loops in the previous blog post. Loops allow us to repeat the same action multiple times.
The key difference between a while and a do-while loop is that the former evaluates the end condition before running the body. In contrast, the latter evaluate it at the end of body execution.
Why does this matter?
While while-loop is evaluating it in the beginning, if the condition is false, the body does not get executed. The do-while ensures body execution once because of the expression evaluation at the end.
A while loop looks like
while(condition) { // If condition is false to start with, this loop will never run. // loop body // counter++}
A do-while loop looks like
do { statement // This statement will execute at least once before the execution of the condition below!}while (condition);
Example of a do while loop:
let i = 0;do { console.log(i); // 0, by the time condition gets evaluated this variable gets printed to the console.} while (i != 0);
Original Link: https://dev.to/bhagatparwinder/javascript-do-while-loop-47il
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To