Nested Loops

Nested loops are loops inside other loops. They are often usedwhen we need to perform repetitive tasks in a multi-dimensional way, such as iterating over the elements of a matrix 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.

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