Loop is a programming tool that repeats a set of instructions until a specified condition meets. When a specified condition is met, the loop stops and the computer moves on to the next part of the program.
The break keyword:
If we want to stop a loop from continuing to execute the block of code even though the original stopping condition hasn’t been met. We can use the break keyword.
The for loop :
Instead of writing out the same code over and over, we can command the computer to repeat a given block of code. We can do this with a for a loop. For loop has an iterator variable that usually appears in all three expressions.
for([initialization]; [condition]; [fintal-expression]) { statement }
Here,
initialization = starts the loop and can also be used to declare an iterator variable.
condition = if the condition evaluates to true the code block will run. Otherwise, if the condition evaluates to false the code block will stop.
final-expression: it is used to update the iterator variable. This occurs before the next evaluation of the condition.
Example:
const arr = ["mozahedul", "Khoka", "rashidul", "shafi"];for (let i = 0; i < arr.length; i++) {console.log(arr[i]);}// Result:mozahedulkhokarashidulshafi
Post a Comment