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

Contenido del 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?

Tarea
test

Swipe to show code editor

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 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?

Tarea
test

Swipe to show code editor

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 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?

Tarea
test

Swipe to show code editor

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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?

Tarea
test

Swipe to show code editor

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt