Nested Loops

Nested loops are loops inside other loops. They are often used when we need to perform repetitive tasks in a multi-dimensional way, such as iterating over the elements of a matrix (e.g., iterate over all the columns of a row before moving on to the next row and repeating the process) or generating combinations of items.

Nested for and while loops

It is possible to nest for and while loops. Nesting means including one for or while loop in another​ for or while loop. The inner for / while loop will execute all its iterations for every single iteration of the outer for / while loop. The syntax for a nested for and nested while loop is as follows:

Create a free account to access the full course.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy