Question

So I am in the process of learning python and I ran into this code that is confusing me.

var = 95
for items in range(0,100):
    if var < 10:
        continue
    elif var == 10:
        print("hit")
    elif var > 10:
        print("passed")
    var = var + 1

I don't understand why it doesn't just print "passed" 5 times... instead it prints it like 100 times.

What exactly is continue doing? If I change var to like (3) will it just "continue" to the next code block?

Was it helpful?

Solution

continue continues to the next loop iteration. See the documentation.

However, in this case it doesn't matter, since the oontinue will never be reached. var starts at 95 and can only increase, so it will never be less than 10.

This code is a bit strange because only the last elif will ever execute. Perhaps you meant to use var somewhere in the loop?

OTHER TIPS

As BrenBarn says, continue just skips the rest of the loop and goes on to the next iteration.

But it doesn't matter when var starts at 95, because that code never gets reached. Let's trace through and see what happens:

var = 95
First loop, items = 0:
   since var (95) > 10:
       print("passed")
   var = var + 1 = 96
Next loop, items = 1
   since var (96) > 10:
       print("passed")
   var = var + 1 = 97
...
100th loop, items = 99
   since var (194) > 10:
       print("passed")
   var = var + 1 = 195

If I change var to like (3) will it just "continue" to the next code block?

No, it continues to the next iteration of the while loop—meaning it skips over the var = var + 1 part. If you want to break out of the loop and go to the next code block, that's break rather than continue.

Now, let's trace through what happens if you start with 3:

var = 3
First loop, items = 0:
   since var (3) < 10:
       continue # skips to the next loop iteration
Second loop, items = 1:
   since var (3) < 10:
       continue # skips to the next loop iteration
...
Last loop, items = 99:
   since var (3) < 10:
       continue # skips to the next loop iteration

Because of the continue, it never gets to the var = var + 1, so it just loops 100 times without doing anything.

Which means if you tried to test it with, say, a print(var) after the loop, it would look an awful lot like it just skipped to the next block of code. But if you put a print(items) there, you'll see that it's 99, not 0. Or, if you print something before continue, you'll see it happen 100 times.

This example is a little weird because you are using var and items differently. Let me rewrite the example to be a little more clear.

for i in range(0,100):

    # If i is less than 10, do nothing.
    if i < 10:
        continue

    # If i equals 10, print "hit"
    if i == 10:
        print "hit"

    # If i is greater than 10, print "passed"
    if i > 10:
        print "passed"

This will output:

hit
passed
passed
... (repeat 87 more times)

The reason your example doesn't only run five times is because you aren't using the variable var in your for loop. Because of the range(0, 100), your for loop is going to happen 100 times with the variable item being incremented each time.

For it to happen five times as you would expect, you would need to use var in your range function as such:

var = 95
for item in range(var, 100):
    if var < 10:
        continue
    elif var == 10:
        print("hit")
    elif var > 10:
        print("passed")
    var = var + 1

continue skips to the next iteration of the loop, without executing any other statements.

In this case, the continue statement won't be hit, since var starts at 95 and is only incremented.

I suspect you meant to reset var to zero when it hit 100?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top