Advertisement
Google Ad Slot: content-top
Java For Loop
A for loop is used to run a block of code repeatedly for a specified number of times, based on a condition. It is one of the most common control structures in JavaScript.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute
}
- Initialization: Sets the starting value for the loop variable.
- Condition: Specifies when the loop should stop (must evaluate to
falseto terminate the loop). - Increment/Decrement: Updates the loop variable after each iteration.
Enhanced For Loop (For-Each Loop):
The for-each loop is used to iterate over arrays or collections without using an index. It's simpler and avoids potential errors with index handling.
Syntax:
for (dataType variable : arrayOrCollection) {
// Code to execute
}