Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Break/Continue in a Nested 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

bookBreak/Continue in a Nested Loop

If the break statement is employed within a nested loop, it will terminate the innermost loop.

Examine the code below:

12345678
# Not allowing the nested loop to work for j == 3 and i == 2 for i in range(4): if i == 2: break for j in range(4): if j == 3: break print(i, j)
copy

How does the code work?

Please note that when the continue statement is encountered inside the loop, it skips all the statements below it and immediately moves to the next iteration.

Examine the code below:

12345678910
numbers_1 = [1, 2, 3] numbers_2 = [1, 2, 4] # Printing the sum of number_1 and number_2 elements, ignoring the sum of the same elements for i in numbers_1: for j in numbers_2: if i == j: continue else: print(i, '+', j, '= ', i + j)
copy

How does the code work?

Tarea

You need to filter out symbols and numbers from the given list.

  1. Configure the outer for loop to iterate through the number of rows in the matrix.
  2. Configure the inner for loop to iterate through the elements in each row of the matrix.
  3. Implement the following conditions: If an element is '#', then continue the loop; else if an element is '!', then break out of the loop; if an element is neither '#' nor '!', then print this element.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

bookBreak/Continue in a Nested Loop

If the break statement is employed within a nested loop, it will terminate the innermost loop.

Examine the code below:

12345678
# Not allowing the nested loop to work for j == 3 and i == 2 for i in range(4): if i == 2: break for j in range(4): if j == 3: break print(i, j)
copy

How does the code work?

Please note that when the continue statement is encountered inside the loop, it skips all the statements below it and immediately moves to the next iteration.

Examine the code below:

12345678910
numbers_1 = [1, 2, 3] numbers_2 = [1, 2, 4] # Printing the sum of number_1 and number_2 elements, ignoring the sum of the same elements for i in numbers_1: for j in numbers_2: if i == j: continue else: print(i, '+', j, '= ', i + j)
copy

How does the code work?

Tarea

You need to filter out symbols and numbers from the given list.

  1. Configure the outer for loop to iterate through the number of rows in the matrix.
  2. Configure the inner for loop to iterate through the elements in each row of the matrix.
  3. Implement the following conditions: If an element is '#', then continue the loop; else if an element is '!', then break out of the loop; if an element is neither '#' nor '!', then print this element.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

bookBreak/Continue in a Nested Loop

If the break statement is employed within a nested loop, it will terminate the innermost loop.

Examine the code below:

12345678
# Not allowing the nested loop to work for j == 3 and i == 2 for i in range(4): if i == 2: break for j in range(4): if j == 3: break print(i, j)
copy

How does the code work?

Please note that when the continue statement is encountered inside the loop, it skips all the statements below it and immediately moves to the next iteration.

Examine the code below:

12345678910
numbers_1 = [1, 2, 3] numbers_2 = [1, 2, 4] # Printing the sum of number_1 and number_2 elements, ignoring the sum of the same elements for i in numbers_1: for j in numbers_2: if i == j: continue else: print(i, '+', j, '= ', i + j)
copy

How does the code work?

Tarea

You need to filter out symbols and numbers from the given list.

  1. Configure the outer for loop to iterate through the number of rows in the matrix.
  2. Configure the inner for loop to iterate through the elements in each row of the matrix.
  3. Implement the following conditions: If an element is '#', then continue the loop; else if an element is '!', then break out of the loop; if an element is neither '#' nor '!', then print this element.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

If the break statement is employed within a nested loop, it will terminate the innermost loop.

Examine the code below:

12345678
# Not allowing the nested loop to work for j == 3 and i == 2 for i in range(4): if i == 2: break for j in range(4): if j == 3: break print(i, j)
copy

How does the code work?

Please note that when the continue statement is encountered inside the loop, it skips all the statements below it and immediately moves to the next iteration.

Examine the code below:

12345678910
numbers_1 = [1, 2, 3] numbers_2 = [1, 2, 4] # Printing the sum of number_1 and number_2 elements, ignoring the sum of the same elements for i in numbers_1: for j in numbers_2: if i == j: continue else: print(i, '+', j, '= ', i + j)
copy

How does the code work?

Tarea

You need to filter out symbols and numbers from the given list.

  1. Configure the outer for loop to iterate through the number of rows in the matrix.
  2. Configure the inner for loop to iterate through the elements in each row of the matrix.
  3. Implement the following conditions: If an element is '#', then continue the loop; else if an element is '!', then break out of the loop; if an element is neither '#' nor '!', then print this element.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 3. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt