I am having problem with understanding while loops. can you help me please? What does ( i ) doing there? Thanks!


11/20/2019 9:38:01 AM


10 Answers

New Answer


Have look at my sample and the explanations: https://code.sololearn.com/c8LG5XZyIU07/?ref=app


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


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


M.Komail, your current code is an infinite loop: while 5 == 5: and there is no break to get out of the loop.


~ swim ~ the user have updated the code now When I answered it was while 1==1


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.


Arsenic The condition specified in while loop is always false in the code example, so the loop never runs.


Arsenic Oh ok, ignore my comment then 😁


i functions as an 'infinitely' incrementing number that gets printed.


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