Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop Details
#3
There are some more statements that you can use with loops, either for loops or while loops. The simplest one is the continue statement, which is sort of the opposite of the break statement. While the break statement goes to the end of the loop, the continue statement goes back to the top of the loop. If the continue statement is in a while loop, the condition is checked again. If it is in a for loop, the next value from the iterator is assigned to the loop variable.

The continue statement is generally used to skipping the processing of certain loop items. Say for example that we suffer from triskaidekaphobia (fear of the number 13). We certainly don't want any singing about 13 bottles of bear on the wall. Note that this happens twice: with 13 after one is taken down and before one is taken down. To skip these line we could use an if and a continue:

for bottle in range(99):
    if bottle in (85, 86):
        continue
    print('{0} bottles of beer on the wall, {0} bottles of beer.'.format(99 - bottle))
    print('Take one down, pass it around, {} bottles of beer on the wall.'.format(98 -bottle))
Now there are no thirteens to worry about.

A bit more confusing is the use of the else statement with loops. First, consider that loops are often used to search for things. Say we want to see if there is a power of seven between two numbers:

for num in range(start, end + 1):   # Note that we use end + 1 so that the last value is end.
    if not num % 7:
        break
print(f'The power of 7 is {num}.')
This works fine if we start is 45 and end is 50. It prints 'The power of 7 is 49.' But if end is 48, it prints 'The power of 7 is 48.' But 48 isn't a power of seven. We could put in a check at the end to be sure we found one:

for num in range(start, end + 1):   # Note that we use end + 1 so that the last value is end.
    if not num % 7:
        break
if not num % 7:
    print(f'The power of 7 is {num}.')
else:
    print('No power of seven was found.')
That works, but there are two problems. First, it's repeating code again. Whenever you repeat code, try to think of a good way not to repeat it. There might not be a good way, but think about it. Second, if the test is complicated, not only are we repeating more code, but the test might not be doable any more. But Python gives us a simpler way to do this (keeping it simple is always a good idea):

for num in range(start, end + 1):   # Note that we use end + 1 so that the last value is end.
    if not num % 7:
        print(f'The power of 7 is {num}.')
        break
else:
    print('No power of seven was found.')
Now we print the power of seven when we find it, right before we break out of the loop. Our warning about no power of seven is now in the else. If you test it, it will work. Why? Because an else statement triggers if a loop ends without a break statement. Another way to say that is the else statement triggers if the loop ends normally. So if we don't find a power of seven, there is no break statement, and we print our warning.

This use of else can be confusing, but remember the example of looking for something. Finding that thing, and using the break statement, is kind of like the True value in a conditional. Not finding that thing is therefore kind of like the False value of a conditional, which triggers the else statement.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Loop Details - by ichabod801 - Sep-13-2019, 06:41 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:42 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:43 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:44 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:47 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:49 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020