Exit the loop when i is 5.......find what is the error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Exit the loop when i is 5.......find what is the error?

i=0 while i<10: if i==5: break print(i) i=i+1

19th Oct 2021, 10:58 AM
Sudipta Dhara
Sudipta Dhara - avatar
5 Answers
+ 3
#Try this i=0 while i<10: print(i) i=i+1 if i==5: break
19th Oct 2021, 12:23 PM
Simba
Simba - avatar
+ 1
is that a qustion or exercise ?
19th Oct 2021, 11:00 AM
ubai
ubai - avatar
+ 1
this is an infinti loop cuz the i will remain 0 u should write i+=1 inside the while block i=0 while i<10: if i==5: break i=i+1 print(i)
19th Oct 2021, 11:51 AM
ubai
ubai - avatar
+ 1
i=0 while i<10: print(i) if i==5: break i=i+1
19th Oct 2021, 12:51 PM
Abhishek Kumar
Abhishek Kumar - avatar
0
the point of a while loop is to give a condition to where the code can get out of the loop if said condition is met. Almost no need for a break statement. Instead of seeing if it equals 5, why not set the while loop to go if 'i' is less than or equal to 5?
19th Oct 2021, 11:07 AM
Slick
Slick - avatar