-3
while b:
   b -= 2

What is the condition in a line written like this? Does it default to while b > 0?

Coder567
  • 51
  • 1
  • 5
  • 2
    Here's a [related question](https://stackoverflow.com/q/39983695/4650297) that might help. – BruceWayne Oct 08 '18 at 21:34
  • 1
    Assuming `b` is a number, it means `while b!=0:` – khelwood Oct 08 '18 at 21:35
  • What happens when you run it? – Athena Oct 08 '18 at 21:38
  • Why has **no one** mentioned that `b` *must be even and positive*, otherwise the code will run in an infinite loop? If this is satisfied, the code will execute until it reaches `0`, which will satisfy the `while` condition and exit the loop. – Sunny Patel Oct 08 '18 at 22:31

3 Answers3

0

Your best bet is to try it. Try running:

b=10

while b:
    print(b)
    b-=2

and see what happens

G. Anderson
  • 5,815
  • 2
  • 14
  • 21
0

Yes, it sure does. b > 0.

b = 10
while b:
    print(b)
    b -= 1

A simple print() statement should let you know when the loop terminates.

Furthermore, this does not mean that the loop will not run for negative b < 0. This means that the loop will terminate then b = 0.

b = -1
while b:
    print(b)
    b -= 1

Goes into an infinite loop.

o12d10
  • 800
  • 3
  • 17
  • 31
0

This basically checks if b is True, inside the while loop it does b -= 2

Spandyie
  • 914
  • 2
  • 11
  • 23