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

Course Content

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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt