+ 1

why output showing 7 also ?

i = 0 while True: i = i + 1 print(i) if i > 6: print("Breaking") break print("Finished")

24th Apr 2021, 7:29 AM
MV Phaneendra
MV Phaneendra - avatar
2 Respostas
+ 3
Hey MV Phaneendra First it will print 7 , then your if-condition will execute First apply condition then execute statement. if i <= 6: print(i) else: print("Breaking") break
24th Apr 2021, 7:32 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 3
You are printing before testing i > 6. Test first then print. That way it will break at 7 but not print 7. Try this: i = 0 while True: i = i + 1 # why not use i += 1? if i > 6: print("Breaking") break print(i) print("Finished")
24th Apr 2021, 8:17 AM
David Ashton
David Ashton - avatar