Deepak Web Tech.pptx
Deepak Web Tech.pptx
Loops in JavaScript
Loops are a fundamental programming concept in JavaScript,
allowing developers to automate repetitive tasks and create
dynamic, interactive applications. This presentation will explore the
different types of loops available in JavaScript and provide practical
examples to help you master this powerful tool.
What are Loops?
1 Repetitive 2 Automation
Execution
Loops enable the Loops automate
execution of a block of repetitive tasks, saving
code repeatedly, until a time and reducing the
specific condition is met. risk of human error.
3 Efficiency
Loops help write more concise and efficient code, reducing
the overall complexity of a program.
Types of Loops in
JavaScript
For Loop While Loop Do-While Loop
Executes a block of code a Executes a block of code as long as Executes a block of code at least
specified number of times, with a a specified condition is true. once, then continues to execute
counter variable that increments or the block as long as a specified
decrements. condition is true.
For Loop
Syntax
1 2 3
Condition
Specify the condition that must be true for the loop to
continue executing.
Initialization
Loop
Condition
for (var i = 0; i < 5; i+ i<5
+)
{
Increment
console.log(i); i++
}
This for loop will execute 5 times, logging the values 0, 1, 2, 3, and
Output
4 to the console.
0, 1, 2, 3, 4
While Loop
Syntax
while(condition)
{//Codes}
Condition Update
Specify the condition that must be true for the loop to The loop counter variable must be updated within the loop
continue executing. to
ensure the condition eventually becomes false.
1 2 3
Execution
The block of code inside the loop will be executed as long
as the condition is true.
var i = 0; Code Example: While
while (i < 5) Loop
{
Initialization Condition
var i = 0; i<5
console.log(i);
i++;
}
Increment Output
i++ 0, 1, 2, 3, 4
This while loop will execute 5 times, logging the values 0, 1, 2, 3, and 4 to the console.
Do-While Loop
Condition Check
The condition is evaluated after the code block is executed, allowing the loop to continue as long as the
condition is true.
Code Example: Do-While
Loop
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
Initialization var i = 0;
Condition i<5
Iteration i++
Output 0, 1, 2, 3, 4
Thank you for your
kind attention