While & Do While Loops in JavaScript

While & Do While statements keep executing a block of statements in a loop until a given condition evaluates to false.

While loop

The Syntax of the While loop is as shown below

While loop executes its body only if the condition is true.

How it works

  1. The loop evaluates its condition.
  2. If the condition is true then the While body executes. If the condition is false loop ends.
  3. Goto to step 2
While loop in Typescript

While Loop Example

Do While Loop

The syntax of do While loop is as shown below.

The do while loop tests its condition after it executes the while body. Hence the while body executes at least once.

How it works

  1. The loop body executes
  2. loop evaluates its condition.
  3. If the condition is true the loop body executes. If the condition is false loop ends.
  4. Goto to step 2
Do While loop in Typescript

Do While Loop Example

Use Break to break out of the loop

We can use the break statement to break out of a loop

Reference

do… while

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top