Nested while LoopNested while Loop

To recap, the while loop statement repeatedly executes a code block while a particular condition is True. We use a while loop when the number iteration is not fixed.

We will learn how to use a while loop inside another while loop.

Look at the code below:

How does the code work?

In the last section, we worked with a matrix. We can do the same using a nested while loop!

Look at the code below:

How does the code work?

Task

Add 1 to every element of the matrix.

  1. Set the outer while loop to work with every row in the matrix.
  2. Set the inner while loop to work with every element of the row in the matrix.
  3. Add 1 to every element.
  4. Print every updated element.

Everything was clear?

Section 3. Chapter 3
toggle bottom row