+ 3
I am having problem with understanding while loops. can you help me please? What does ( i ) doing there? Thanks!
10 Antworten
+ 9
Have look at my sample and the explanations:
https://code.sololearn.com/c8LG5XZyIU07/?ref=app
+ 8
M.KOMAIL "i" in that code is acting as counter. Because the condition that you have specified in while loop is always true so your loop should run forever
To counter that we are evaluating "i" inside the loop (when i becomes greater or equal to 5, then the loop terminates
If it's still not clear then do let me know I will try to make it more clear
+ 5
Without i, the loop would never break. i is what makes the loop break, I don't really think there is something to explain here, you probably should just review the while loop course here on sololearn
+ 5
M.Komail, your current code is an infinite loop:
while 5 == 5:
and there is no break to get out of the loop.
+ 5
M.Komail#here i is the infinite loop...it never stops..because the condition never fails. As 5is always equal to 5...and i is the counter variable used..it just used to increment the loop..aiding the loop to continue till the condition fails..but it never happens here..becaz it is an infinite loop by default whose condition never fails.
+ 3
~ swim ~ the user have updated the code now
When I answered it was while 1==1
+ 3
While loops keep running while a condition is true. In parentheses, you put your condition. (i) is the condition, and it is always True. So, your loop will run forever since (i) is true
+ 2
i functions as an 'infinitely' incrementing number that gets printed.