Course Content
Learn Python from Scratch
Learn Python from Scratch
while 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.
# 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
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.
Thanks for your feedback!
while 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.
# 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
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.
Thanks for your feedback!
while 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.
# 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
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.
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.
# 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
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.