Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
while loop (2/2) | Loops
Learn Python from Scratch
course content

Course Content

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

bookwhile loop (2/2)

As we mentioned before, sometimes we don't exactly know the stopping condition. In that case, we first need to start an infinite loop and then specify the stopping condition. If you want to start an infinite loop, use while True.

There are two important commands in Python: break and continue. break will break the loop and go outside loop to the next line. continue will skip all the rest code inside loop and return at its beginning.

For example, imagine we have number 3 and want to reach number 6. All we can do - is to add 2 and subtract 1.

12345678910111213141516
# starting number a = 3 # let's construct while loop while True: # infinite loop if a == 6: # stopping condition print("Number", a, "reached") break # this will break loop elif a < 6: # if less, than add 2 a = a + 2 print("Adding 2... Reached", a) continue # returning to loop start else: # if greater, then subtract 1 a = a - 1 print("Subtracting 1... Reached", a) continue # returning to loop start
copy

If we didn't specify break in the first if, then we got an infinite loop, because 6 is not satisfying any other condition but the first one. In that case, it printed us "Number 6 reached" infinite times.

Task

Using while loop and continue/break commands construct the next program:

Given number a = -7. At each step if the number is negative, you need to print "a is negative, and =" and value, and then add 4 and continue the loop. If the number is positive, you need to print "a is positive, and =" and its value after = sign. In that case you need to subtract 3 and continue the loop. If the number is zero - you need to print "Finally a is zero!" and break the loop.

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 6. Chapter 2
toggle bottom row

bookwhile loop (2/2)

As we mentioned before, sometimes we don't exactly know the stopping condition. In that case, we first need to start an infinite loop and then specify the stopping condition. If you want to start an infinite loop, use while True.

There are two important commands in Python: break and continue. break will break the loop and go outside loop to the next line. continue will skip all the rest code inside loop and return at its beginning.

For example, imagine we have number 3 and want to reach number 6. All we can do - is to add 2 and subtract 1.

12345678910111213141516
# starting number a = 3 # let's construct while loop while True: # infinite loop if a == 6: # stopping condition print("Number", a, "reached") break # this will break loop elif a < 6: # if less, than add 2 a = a + 2 print("Adding 2... Reached", a) continue # returning to loop start else: # if greater, then subtract 1 a = a - 1 print("Subtracting 1... Reached", a) continue # returning to loop start
copy

If we didn't specify break in the first if, then we got an infinite loop, because 6 is not satisfying any other condition but the first one. In that case, it printed us "Number 6 reached" infinite times.

Task

Using while loop and continue/break commands construct the next program:

Given number a = -7. At each step if the number is negative, you need to print "a is negative, and =" and value, and then add 4 and continue the loop. If the number is positive, you need to print "a is positive, and =" and its value after = sign. In that case you need to subtract 3 and continue the loop. If the number is zero - you need to print "Finally a is zero!" and break the loop.

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 6. Chapter 2
toggle bottom row

bookwhile loop (2/2)

As we mentioned before, sometimes we don't exactly know the stopping condition. In that case, we first need to start an infinite loop and then specify the stopping condition. If you want to start an infinite loop, use while True.

There are two important commands in Python: break and continue. break will break the loop and go outside loop to the next line. continue will skip all the rest code inside loop and return at its beginning.

For example, imagine we have number 3 and want to reach number 6. All we can do - is to add 2 and subtract 1.

12345678910111213141516
# starting number a = 3 # let's construct while loop while True: # infinite loop if a == 6: # stopping condition print("Number", a, "reached") break # this will break loop elif a < 6: # if less, than add 2 a = a + 2 print("Adding 2... Reached", a) continue # returning to loop start else: # if greater, then subtract 1 a = a - 1 print("Subtracting 1... Reached", a) continue # returning to loop start
copy

If we didn't specify break in the first if, then we got an infinite loop, because 6 is not satisfying any other condition but the first one. In that case, it printed us "Number 6 reached" infinite times.

Task

Using while loop and continue/break commands construct the next program:

Given number a = -7. At each step if the number is negative, you need to print "a is negative, and =" and value, and then add 4 and continue the loop. If the number is positive, you need to print "a is positive, and =" and its value after = sign. In that case you need to subtract 3 and continue the loop. If the number is zero - you need to print "Finally a is zero!" and break the loop.

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!

As we mentioned before, sometimes we don't exactly know the stopping condition. In that case, we first need to start an infinite loop and then specify the stopping condition. If you want to start an infinite loop, use while True.

There are two important commands in Python: break and continue. break will break the loop and go outside loop to the next line. continue will skip all the rest code inside loop and return at its beginning.

For example, imagine we have number 3 and want to reach number 6. All we can do - is to add 2 and subtract 1.

12345678910111213141516
# starting number a = 3 # let's construct while loop while True: # infinite loop if a == 6: # stopping condition print("Number", a, "reached") break # this will break loop elif a < 6: # if less, than add 2 a = a + 2 print("Adding 2... Reached", a) continue # returning to loop start else: # if greater, then subtract 1 a = a - 1 print("Subtracting 1... Reached", a) continue # returning to loop start
copy

If we didn't specify break in the first if, then we got an infinite loop, because 6 is not satisfying any other condition but the first one. In that case, it printed us "Number 6 reached" infinite times.

Task

Using while loop and continue/break commands construct the next program:

Given number a = -7. At each step if the number is negative, you need to print "a is negative, and =" and value, and then add 4 and continue the loop. If the number is positive, you need to print "a is positive, and =" and its value after = sign. In that case you need to subtract 3 and continue the loop. If the number is zero - you need to print "Finally a is zero!" and break the loop.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 6. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt