What Do Nested for Loops Do

What Do Nested for Loops Do

A nested loop has one loop inside of another. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. … In each iteration of the outer loop, the inner loop will be re-started.

What are nested for loops used for?

The inner loop is nested inside the outer loop. Nested loops are useful when for each pass through the outer loop, you need to repeat some action on the data in the outer loop. For example, you read a file line by line and for each line you must count how many times the word “the” is found.

Is it bad to have nested for loops?

Nested loops are frequently (but not always) bad practice, because they’re frequently (but not always) overkill for what you’re trying to do. In many cases, there’s a much faster and less wasteful way to accomplish the goal you’re trying to achieve.

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.