Why the loop goes on to infinity , I have put limits via n<6 , one more thing , the code prints 111111... . The output that I expect is 12345.
#include <stdio.h>
//Compiler version gcc 6.3.0
int main()
{
int n=1;
do{
while(n<6)
printf("%d",n);
n++;
}
while(n<6);
return 0;
}
Why this is an infinite loop?
Because this:
do{
while(n<6)
printf("%d",n);
n++;
}
...
Is actually this:
do{
while(n<6) {
printf("%d",n);
}
n++;
}
...
The code will never escape the "single statement while loop" just under the do
. I would suggest that deleting it so that you only have one line that says while(n<6)
, just above the return
, will make your program function as you expect