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

Course Content

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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your 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?

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt