Mastering the Loop Machine

For Loop

Let's Discover What You Will Achieve Today !

What is a "For" loop in javascript?

1

2

How does the "For" loop operate?

Imagine you’re watering plants .

Imagine you’re watering plants .

You repeat the same action a fixed number of times or until a condition is satisfied.

You don’t stop in the middle,
and you don’t water the same plant forever.

You water one plant, then move to the next,
and keep doing it until all plants are watered.

In programming, many tasks work the same way.

  •  Sometimes a statement needs to run multiple times,
  • sometimes until a condition changes,
  • and sometimes for a fixed number of steps.

To handle this repetition smoothly and avoid writing the same code again and again,
JavaScript provides looping statements.

 What are loops in javascript?

  • Loops in Javascript are used to repeat the same code again and again while a condition is true.
  • It  stops when the condition becomes false.

Majorly used Loops

While Loop

Do-while Loop

For Loop

Benefits of Using Loops

Gives better control: You can decide how many times the code should run.

Saves time and effort: You don’t have to write the same code many times.

Makes code easier to read: Repeated work is written in one place.

For Loop

  • A for loop is used when the number of repetitions is known.
  • It runs step by step and stops when the condition becomes false.

Imagine climbing stairs and counting 10 steps.
You move one step at a time, counting to 10, then stop.

 In the same way, a for loop repeats an action step by step until the count is complete.

Syntax:

for (initialization; termination condition; increment/decrement){

//Set of statements to be executed repeatedly

}

Flow of for loop

START

CONDITION

 INITIALIZATION

True

False

STATEMENT EXECUTION

UPDATE

END

But Before This Let's Understand The Structure for "For" Loop

for (initialization; condition; increment) {
    // code block to execute
}

Here the "For" is a reserved keyword

Checks if the loop should continue before each iteration

Sets the starting point of the loop

Updates the loop counter at the end of each iteration

Summary

Increment updates the loop counter after each run

Initialization runs only once

The loop runs until the condition becomes false

It has three parts: initialization, condition, and increment/decrement

A for loop is used to repeat a block of code a fixed number of times

1

2

3

2

4

5

Quiz

What is the main purpose of a for loop in JavaScript?

A. To define a function

B. To declare variables

C. To repeat a block of code

D. To handle errors

Quiz-Answer

A. To define a function

B. To declare variables

C. To repeat a block of code

D. To handle errors

What is the main purpose of a for loop in JavaScript?

Mastering the loop machine: For loop

By Content ITV

Mastering the loop machine: For loop

  • 112