Loops: Repeating Actions with for, while, and do-while
Loops are fundamental control flow structures in JavaScript (and programming in general) that allow you to execute a block of code repeatedly. This is incredibly useful for tasks like iterating over collections of data, performing calculations multiple times, or waiting for a specific condition to be met.
JavaScript provides several types of loops, each suited for different scenarios: for loops, while loops, and do-while loops.
1. The for Loop
The for loop is typically used when you know, or can determine, the number of times you want the loop to run. It's concise and combines the loop's initialization, condition, and iteration step into a single line.
Syntax
for (initialization; condition; afterthought) {
// Code to be executed in each iteration
}
initialization: Executed once before the loop starts. Typically used to declare and initialize a loop counter variable (e.g.,let i = 0;).condition: Evaluated before each iteration. If it evaluates totrue, the loop continues. Iffalse, the loop terminates.afterthought: Executed after each iteration (after the code block runs). Typically used to increment or decrement the loop counter (e.g.,i++).
Examples
2. The while Loop
The while loop is used when you don't know the exact number of iterations beforehand, but you want the loop to continue as long as a certain condition remains true. The condition is evaluated before each iteration.
Syntax
while (condition) {
// Code to be executed as long as the condition is true
// Remember to change the condition within the loop to avoid infinite loops!
}
condition: Evaluated before each iteration. If it'strue, the code block executes. Iffalse, the loop terminates.- Important: You must include code within the loop block that eventually makes the
conditionfalseto avoid an infinite loop, which will crash your program.
Examples
3. The do-while Loop
The do-while loop is very similar to the while loop, but with one key difference: its code block is guaranteed to execute at least once, because the condition is evaluated after the first iteration.
Syntax
do {
// Code to be executed at least once, and then repeatedly
// as long as the condition is true
} while (condition); // Note the semicolon here!
- The code inside the
doblock executes first. - Then, the
conditionis evaluated. Iftrue, the loop repeats. Iffalse, the loop terminates.
Examples
4. break and continue Statements in Loops
These two keywords provide additional control over loop execution.
break
The break statement immediately terminates the innermost loop (or switch statement) and transfers control to the statement immediately following the loop.
continue
The continue statement skips the rest of the current iteration of the loop and proceeds to the next iteration. For for loops, it jumps to the afterthought expression. For while/do-while loops, it jumps to the condition check.
5. Choosing the Right Loop
forloop: Best when you know the number of iterations or are iterating over an array/string with a fixed length.whileloop: Best when you don't know the exact number of iterations, but the loop needs to continue as long as a specific condition is true (e.g., waiting for user input, processing data from a stream).do-whileloop: Best when you need the loop's code block to execute at least once, regardless of the initial condition, and then repeat based on that condition (e.g., input validation where you need to get input at least once).
Exercise: Loop Challenges
Solve the following challenges using the appropriate loop type (for, while, or do-while) and incorporate break or continue if useful.

