I am having problem with understanding while loops. can you help me please? What does ( i ) doing there? Thanks! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

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

https://code.sololearn.com/cPHq47wUortL/?ref=app

20th Nov 2019, 9:38 AM
M.KOMAIL
M.KOMAIL - avatar
8 Answers
+ 9
Have look at my sample and the explanations: https://code.sololearn.com/c8LG5XZyIU07/?ref=app
20th Nov 2019, 10:27 AM
Lothar
Lothar - avatar
+ 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
20th Nov 2019, 9:47 AM
Arsenic
Arsenic - avatar
+ 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
20th Nov 2019, 9:45 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 5
M.Komail, your current code is an infinite loop: while 5 == 5: and there is no break to get out of the loop.
20th Nov 2019, 2:43 PM
Lothar
Lothar - avatar
+ 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.
21st Nov 2019, 2:41 PM
Devi Kamaraj..😉
Devi Kamaraj..😉 - avatar
+ 3
~ swim ~ the user have updated the code now When I answered it was while 1==1
20th Nov 2019, 12:24 PM
Arsenic
Arsenic - avatar
+ 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
21st Nov 2019, 11:53 PM
OnesAndZeroes10
OnesAndZeroes10 - avatar
+ 2
i functions as an 'infinitely' incrementing number that gets printed.
21st Nov 2019, 1:07 PM
Sonic
Sonic - avatar