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

Now, we'll attempt the same task we previously accomplished with the for loop—finding a number in the list and terminating the loop once it's located.

Examine the code below:

12345678910
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
copy

How does the code work?

Great!

Please be aware that the continue statement skips a block of code within the loop for the current iteration only. Now, let's count the occurrences of 11s in the code using continue.

Examine the code below:

123456789101112131415
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
copy

How does the code work?

Tarefa

Halt the loop when encountering a negative value.

  1. Initialize a while loop, employing i to interact with numbers.
  2. Increment the value of i.
  3. Display a message along with the negative element.
  4. Employ break and continue as needed.

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 2. Capítulo 5
toggle bottom row

bookBreak/Continue in a while Loop

Now, we'll attempt the same task we previously accomplished with the for loop—finding a number in the list and terminating the loop once it's located.

Examine the code below:

12345678910
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
copy

How does the code work?

Great!

Please be aware that the continue statement skips a block of code within the loop for the current iteration only. Now, let's count the occurrences of 11s in the code using continue.

Examine the code below:

123456789101112131415
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
copy

How does the code work?

Tarefa

Halt the loop when encountering a negative value.

  1. Initialize a while loop, employing i to interact with numbers.
  2. Increment the value of i.
  3. Display a message along with the negative element.
  4. Employ break and continue as needed.

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 2. Capítulo 5
toggle bottom row

bookBreak/Continue in a while Loop

Now, we'll attempt the same task we previously accomplished with the for loop—finding a number in the list and terminating the loop once it's located.

Examine the code below:

12345678910
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
copy

How does the code work?

Great!

Please be aware that the continue statement skips a block of code within the loop for the current iteration only. Now, let's count the occurrences of 11s in the code using continue.

Examine the code below:

123456789101112131415
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
copy

How does the code work?

Tarefa

Halt the loop when encountering a negative value.

  1. Initialize a while loop, employing i to interact with numbers.
  2. Increment the value of i.
  3. Display a message along with the negative element.
  4. Employ break and continue as needed.

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!

Now, we'll attempt the same task we previously accomplished with the for loop—finding a number in the list and terminating the loop once it's located.

Examine the code below:

12345678910
numbers = [2, 3, 4, 11, 5] i = 0 # Breaking the loop, if 11 is found while i < len(numbers): if numbers[i] == 11: print('11 is here!') break else: i = i + 1
copy

How does the code work?

Great!

Please be aware that the continue statement skips a block of code within the loop for the current iteration only. Now, let's count the occurrences of 11s in the code using continue.

Examine the code below:

123456789101112131415
numbers = [2, 11, 4, 11, 5] i = -1 counter = 0 # Count all 11 in the list # We are using len(numbers) - 1 to break the loop without being 'out of range' while i < len(numbers) - 1: i = i + 1 if numbers[i] != 11: continue else: print('11 is here!') counter += 1 print("Total number of '11': ", counter)
copy

How does the code work?

Tarefa

Halt the loop when encountering a negative value.

  1. Initialize a while loop, employing i to interact with numbers.
  2. Increment the value of i.
  3. Display a message along with the negative element.
  4. Employ break and continue as needed.

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