Course Content
Python Loops Tutorial
Python Loops Tutorial
Break/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:
# 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)
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:
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)
How does the code work?
Task
You need to filter out symbols and numbers from the given list.
- Configure the outer
for
loop to iterate through the number of rows in the matrix. - Configure the inner
for
loop to iterate through the elements in each row of the matrix. - Implement the following conditions: If an element is
'#'
, thencontinue
the loop; else if an element is'!'
, thenbreak
out of the loop; if an element is neither'#'
nor'!'
, then print this element.
Thanks for your feedback!
Break/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:
# 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)
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:
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)
How does the code work?
Task
You need to filter out symbols and numbers from the given list.
- Configure the outer
for
loop to iterate through the number of rows in the matrix. - Configure the inner
for
loop to iterate through the elements in each row of the matrix. - Implement the following conditions: If an element is
'#'
, thencontinue
the loop; else if an element is'!'
, thenbreak
out of the loop; if an element is neither'#'
nor'!'
, then print this element.
Thanks for your feedback!
Break/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:
# 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)
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:
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)
How does the code work?
Task
You need to filter out symbols and numbers from the given list.
- Configure the outer
for
loop to iterate through the number of rows in the matrix. - Configure the inner
for
loop to iterate through the elements in each row of the matrix. - Implement the following conditions: If an element is
'#'
, thencontinue
the loop; else if an element is'!'
, thenbreak
out of the loop; if an element is neither'#'
nor'!'
, then print this element.
Thanks for your feedback!
If the break
statement is employed within a nested loop, it will terminate the innermost loop.
Examine the code below:
# 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)
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:
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)
How does the code work?
Task
You need to filter out symbols and numbers from the given list.
- Configure the outer
for
loop to iterate through the number of rows in the matrix. - Configure the inner
for
loop to iterate through the elements in each row of the matrix. - Implement the following conditions: If an element is
'#'
, thencontinue
the loop; else if an element is'!'
, thenbreak
out of the loop; if an element is neither'#'
nor'!'
, then print this element.