DEV Community

Anik Saha
Anik Saha

Posted on

1

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/

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More