+ 2

{ANSWERED}What does the while true go on

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

26th Apr 2022, 8:10 PM
Lz1234
13 Answers
+ 4
On each iteration, i is increased by one and the loop would go on infinitely (while True) if there wasn't the if-statement. If i gets larger or equal 5, the break-statement stops the loop.
26th Apr 2022, 8:49 PM
Lisa
Lisa - avatar
+ 3
Lz1234 While true sets a condition that will continue to iterate until you break the loop as you have done in your example. You could set a condition that will ultimately become false, which would break the loop without the 'break' statement. IE: i = 0 while i < 6: print(i) i = i + 1 print("Finished")
26th Apr 2022, 8:56 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Does every boolean code need to start with While true or just by for loops or while loops
26th Apr 2022, 8:51 PM
Lz1234
+ 2
26th Apr 2022, 8:58 PM
Lz1234
+ 1
Go to Code section, click +, select the programming language, insert your code, save. Then come back to this thread, click +, Insert Code, sort for My Code Bits, select your code. Check the indentation. I assume that an erroneous indentation could be the issue
26th Apr 2022, 8:43 PM
Lisa
Lisa - avatar
26th Apr 2022, 8:44 PM
Lz1234
+ 1
Look okay to me. Is it the output you were expecting?
26th Apr 2022, 8:46 PM
Lisa
Lisa - avatar
+ 1
It works just trying to understand the code trying to undrestand what word it goes on
26th Apr 2022, 8:46 PM
Lz1234
+ 1
Thanks
26th Apr 2022, 8:51 PM
Lz1234
+ 1
The condition in the head of the while loop could also be a comparison: while i >= 5: i += 1 print(i)
26th Apr 2022, 8:54 PM
Lisa
Lisa - avatar
0
I think the indentation got messed up. Please put your code in a script on sololearn playground rather than copying it in the description.
26th Apr 2022, 8:36 PM
Lisa
Lisa - avatar
0
That depends on the context... it should be something that can be evaluated to a boolean
26th Apr 2022, 8:57 PM
Lisa
Lisa - avatar
27th Apr 2022, 10:44 PM
Muhammad Mehran Bin Azam
Muhammad Mehran Bin Azam - avatar