PRO
M.
asked

Expert
Abhay Jajodia answered
It’s not a bug. Your count never changes, so count <= 3 stays true forever.
Buggy code:
#include <stdio.h>
int main() {
int count = 1;
while (count <= 3) {
printf("I am inside a loop.\n");
printf("Looping is interesting.\n");
}
return 0;
}
Output (repeats forever):
I am inside a loop.
Looping is interesting.
...
Fix:
#include <stdio.h>
int main() {
int count = 1;
while (count <= 3) {
printf("I am inside a loop.\n");
printf("Looping is interesting.\n");
count = count + 1;
}
return 0;
}
Output:
I am inside a loop.
Looping is interesting.
I am inside a loop.
Looping is interesting.
I am inside a loop.
Looping is interesting.
If you have more questions, I am here to help.
C
This question was asked as part of the Learn C Programming course.
Our Experts
Sanjeev PanditPrincipal Engineer
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Anuj ShresthaTech Lead
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer