Course Content
Python Loops Tutorial
Python Loops Tutorial
Break/Continue in a for Loop
The break
statement is employed to prematurely exit a loop. In our previous task, when searching for the number 8 in the list, if we wish to conclude the search as soon as we find the element, we can utilize break
.
Examine the code below:
How does the code work?
Now, let's work on counting all occurrences of 8 in our list using the continue
statement. The continue
statement allows us to skip a specific block of code for the current iteration of the loop only.
Examine the code below:
How does the code work?
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.
Everything was clear?
Break/Continue in a for Loop
The break
statement is employed to prematurely exit a loop. In our previous task, when searching for the number 8 in the list, if we wish to conclude the search as soon as we find the element, we can utilize break
.
Examine the code below:
How does the code work?
Now, let's work on counting all occurrences of 8 in our list using the continue
statement. The continue
statement allows us to skip a specific block of code for the current iteration of the loop only.
Examine the code below:
How does the code work?
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.
Everything was clear?
Break/Continue in a for Loop
The break
statement is employed to prematurely exit a loop. In our previous task, when searching for the number 8 in the list, if we wish to conclude the search as soon as we find the element, we can utilize break
.
Examine the code below:
How does the code work?
Now, let's work on counting all occurrences of 8 in our list using the continue
statement. The continue
statement allows us to skip a specific block of code for the current iteration of the loop only.
Examine the code below:
How does the code work?
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.
Everything was clear?
The break
statement is employed to prematurely exit a loop. In our previous task, when searching for the number 8 in the list, if we wish to conclude the search as soon as we find the element, we can utilize break
.
Examine the code below:
How does the code work?
Now, let's work on counting all occurrences of 8 in our list using the continue
statement. The continue
statement allows us to skip a specific block of code for the current iteration of the loop only.
Examine the code below:
How does the code work?
Task
To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:
- Configure a
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition to check if the result of the multiplication (
counter
) is going to reach or exceed 100. If so, break out of the loop; otherwise, continue multiplying by the next element. - Print the value of the
counter
.