DEV Community

Anik Saha
Anik Saha

Posted on

JavaScript Loops

Hello everyone, welcome to the third blog of the beginner series.
In previous blog we talked about data type method, today we will be discussing basics on loops.

What is loop?

Loop is a sequence of instruction that is continuously repeated until a certain condition is reached.

Types of loops :

There are three types of loops, "while loops", "for loops", "do-while loop".

**while Loops:

**
while loops continue to run until a condition is true.

Syntax :

Image description

The purpose of a while loop is to execute a statement or code block continuously repeated as long as an expression is true. Once the expression became false, the loop terminates.

Image description

Output:

Image description

do-while Loop:

do-while loop is similar to the while loop except that the condition check happens at the end of the loop.

Syntax:

Image description

If the condition is true the loop will repeated. Or loop will always be executed at least once , even if the condition is false.

Image description

Since the do block is run first without checking the condition, this can cause unexpected issues if you're not careful.

Image description

For Loop:

for loop are the most common type of loop. for loop repeats until a specific condition evaluates to false.

Image description

The initial expression , if any is executed. This expression usually initialize one or more loop.
The condition Expression is ending of the loop. If the expression is true , the loop statements execute.
To execute multiple statements use block statement.

Here the example of for loops. The most common loop -

Image description

Output:

Image description

Ending Notes

This is all about loops, thanks for your reading till the end.

You will also check this link :
https://www.w3schools.com/js/

Top comments (0)