Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested while Loop | Nested Loops
Python Loops Tutorial
course content

Contenido del Curso

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

Nested while Loop

To summarize, the while loop statement executes a code block repeatedly as long as a specific condition remains True. We opt for a while loop when the number of iterations isn't predetermined.

Now, let's delve into employing a while loop within another while loop.

Examine the code below:

1234567891011
# Printing numbers from 0 to 4 for 5 times i = 0 # Setting outer while loop while i < 5: j = 0 # Setting inner while loop while j < 5: print(j, end='') j += 1 i += 1 print('')
copy

How does the code work?

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

Examine the code below:

1234567891011121314
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing all elements of the matrix i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): print(matrix[i][j], end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 3. Capítulo 3
toggle bottom row

Nested while Loop

To summarize, the while loop statement executes a code block repeatedly as long as a specific condition remains True. We opt for a while loop when the number of iterations isn't predetermined.

Now, let's delve into employing a while loop within another while loop.

Examine the code below:

1234567891011
# Printing numbers from 0 to 4 for 5 times i = 0 # Setting outer while loop while i < 5: j = 0 # Setting inner while loop while j < 5: print(j, end='') j += 1 i += 1 print('')
copy

How does the code work?

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

Examine the code below:

1234567891011121314
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing all elements of the matrix i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): print(matrix[i][j], end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 3. Capítulo 3
toggle bottom row

Nested while Loop

To summarize, the while loop statement executes a code block repeatedly as long as a specific condition remains True. We opt for a while loop when the number of iterations isn't predetermined.

Now, let's delve into employing a while loop within another while loop.

Examine the code below:

1234567891011
# Printing numbers from 0 to 4 for 5 times i = 0 # Setting outer while loop while i < 5: j = 0 # Setting inner while loop while j < 5: print(j, end='') j += 1 i += 1 print('')
copy

How does the code work?

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

Examine the code below:

1234567891011121314
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing all elements of the matrix i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): print(matrix[i][j], end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

To summarize, the while loop statement executes a code block repeatedly as long as a specific condition remains True. We opt for a while loop when the number of iterations isn't predetermined.

Now, let's delve into employing a while loop within another while loop.

Examine the code below:

1234567891011
# Printing numbers from 0 to 4 for 5 times i = 0 # Setting outer while loop while i < 5: j = 0 # Setting inner while loop while j < 5: print(j, end='') j += 1 i += 1 print('')
copy

How does the code work?

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

Examine the code below:

1234567891011121314
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing all elements of the matrix i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): print(matrix[i][j], end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

Tarea

Increment every element in the matrix by 1.

  1. Configure the outer while loop to iterate through each row in the matrix.
  2. Configure the inner while loop to iterate through each element in the row of the matrix.
  3. Add 1 to every element.
  4. Display each updated element.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 3. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt