Content ITV PRO
This is Itvedant Content department
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.
To handle this repetition smoothly and avoid writing the same code again and again,
JavaScript provides looping statements.
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.
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
}
START
CONDITION
INITIALIZATION
True
False
STATEMENT EXECUTION
UPDATE
END
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?
By Content ITV