0
t=int(input())
while t:
    t-=1
    n,m=map(int,input().split())
    print((n-1)*(m-1))

while t here makes me confused , is it always true ? i have no idea to describe it.I have never seen this .Could you explain what is going on here with explaination , please ,thank you!

2
  • 1
    Every iteration, t decreases by 1 in t-=1. It will exit when it reaches 0. Apr 14, 2020 at 10:59
  • t depends on the user of your input. first if the user type a word it will cause error, secondly if he types numbers 0 is evaluated as false, any other int is evaluated as true, so it will keep evaluating true until t become 0 Apr 14, 2020 at 11:00

4 Answers 4

1

0 is False is a boolean context, any other number is True.

So here, the loop continues until t is reduced to 0.

0

t will be True unless it equals to 0 (which is logical False in Python).

In your case, if t is positive, it will be decreased by 1 and the while loop will stop after t=0.

But if the input will be negative number, the loop will run infinitely.

0

In your case while accept a integer t as the condition. Where the loop iterates until t is 0.

while expression:
   statement(s)

The condition here may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

0

while loop works when the parameter it contains is true. So in your example, it will evaluate the value of t this way: bool(t). If it will be False then the loop will stop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.