-2

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?

Capurnicus
  • 8,171
  • 5
  • 18
  • 11
  • 3
    did you mean to write `for items in range(var,100)`? – Stephan Dollberg Dec 19 '12 at 21:16
  • @bamboon: That still wouldn't make much sense, since it would just be equivalent to `range(95, 100)`. Modifying `var` inside the loop won't affect what it's set to when the `range` is initialized. – BrenBarn Dec 19 '12 at 21:19
  • @BrenBarn sure, but that would print "passed" 5 times. – Stephan Dollberg Dec 19 '12 at 21:22
  • @bamboon: Heh, that is true. I just mean the code would still be puzzling, since it would still include two unreachable conditions for no reason. – BrenBarn Dec 19 '12 at 21:23
  • I get what continue is doing now at least lol. I still dont understand why it doesn't print "passed" 5 times though. Is it not counting up from 95 to 100? – Capurnicus Dec 19 '12 at 21:26
  • Nvm, I see what you mean bamboon. (var,100) gives what i was expecting to see. – Capurnicus Dec 19 '12 at 21:29
  • @user1901780: See my answer; it explains why it's actually counting up from 95 to 194—and why, if you start at 3, it will just stay at 3 forever. – abarnert Dec 19 '12 at 21:32

5 Answers5

6

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?

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
2

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

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
Michael Davis
  • 2,350
  • 2
  • 21
  • 29
  • `range(0,100)` is only 100 times, not 101. Also, it's not going to repeat 89 more times, but 87. – abarnert Dec 19 '12 at 21:34
  • You are correct on range(0,100) being 100, not 101. For repeating 89 times I meant 89 total. I changed it to read 87 more times to be more clear. – Michael Davis Dec 19 '12 at 22:16
0

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.

jeffknupp
  • 5,966
  • 3
  • 28
  • 29
0

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

Boojum
  • 6,592
  • 1
  • 30
  • 34