Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Break/Continue in a for Loop | The for Loop
Python Loops Tutorial
course content

Conteúdo do Curso

Python Loops Tutorial

Python Loops Tutorial

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

bookBreak/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:

123456789
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Finishing the loop when 8 is found for i in numbers: if i == 8: print('Here is 8!') break else: print('This num is not 8')
copy

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:

12345678910
numbers = [2, 3, 8, 5, 6, 7, 8, 11, 8] counter = 0 # Counting all 8 in the list for i in numbers: if i == 8: counter += 1 else: continue print(counter)
copy

How does the code work?

Tarefa

To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:

  1. Configure a for loop to operate on the numbers, using i as an element of the list.
  2. 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.
  3. Print the value of the counter.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 6
toggle bottom row

bookBreak/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:

123456789
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Finishing the loop when 8 is found for i in numbers: if i == 8: print('Here is 8!') break else: print('This num is not 8')
copy

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:

12345678910
numbers = [2, 3, 8, 5, 6, 7, 8, 11, 8] counter = 0 # Counting all 8 in the list for i in numbers: if i == 8: counter += 1 else: continue print(counter)
copy

How does the code work?

Tarefa

To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:

  1. Configure a for loop to operate on the numbers, using i as an element of the list.
  2. 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.
  3. Print the value of the counter.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 6
toggle bottom row

bookBreak/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:

123456789
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Finishing the loop when 8 is found for i in numbers: if i == 8: print('Here is 8!') break else: print('This num is not 8')
copy

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:

12345678910
numbers = [2, 3, 8, 5, 6, 7, 8, 11, 8] counter = 0 # Counting all 8 in the list for i in numbers: if i == 8: counter += 1 else: continue print(counter)
copy

How does the code work?

Tarefa

To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:

  1. Configure a for loop to operate on the numbers, using i as an element of the list.
  2. 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.
  3. Print the value of the counter.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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:

123456789
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Finishing the loop when 8 is found for i in numbers: if i == 8: print('Here is 8!') break else: print('This num is not 8')
copy

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:

12345678910
numbers = [2, 3, 8, 5, 6, 7, 8, 11, 8] counter = 0 # Counting all 8 in the list for i in numbers: if i == 8: counter += 1 else: continue print(counter)
copy

How does the code work?

Tarefa

To multiply the elements of the list and stop the program when the next multiplication exceeds 100, follow these steps:

  1. Configure a for loop to operate on the numbers, using i as an element of the list.
  2. 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.
  3. Print the value of the counter.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 6
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt