JavaScript While Loop - Java Script

What is JavaScript While loop?

While writing a code, you may come across a situation where you require to execute a block of code again and again. To minimize the number of lines of your code, you may need to write loop statements

JavaScript mainly supports three different types of loops to lesser down the pressure of programming.

They are:

  • The while loop.
  • The Do-While loop.
  • The while Loop


The purpose of a while loop is to execute a statement or a block of codefrequentlyas long as the test condition results true. Once the test condition results false, the loop terminates.

Flow Chart of While Loop:

The flow chart of while loop is shown below−
Flow Chart of While Loop
Syntax:
Below is the syntax of while loop
Example:
Look at the below example to implement while loop.
This will produce the below output:
Flow Chart of While Loop

What is Java Script do...while Loop?

The do...while loop is similar to the while loop except that it executes the code once and then checks the condition.This means that code block will always be executed at least once, even though the condition returns false.

Flow Chart of do.. while

The flow chart of do while loop is shown below−

Syntax:
Flow Chart of do.. while
Below is the syntax for do-while loop in JavaScript

Note –Make sure you give a semi colon at the end of the do...while loop.

Example:

Try the below example which illustrates the implementation of do-while loop in JavaScript.

This will produce the below output:

Flow Chart of do.. while

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics