Problem with do while

Joined
Dec 10, 2022
Messages
2
Reaction score
0
Greetings guys,
While doing do while exercice

#include <stdio.h>

int main()

{
int i=0;
do
{
printf("%d hello\n",i);
i=i+1;

}

while(i<=5);



it works well but when I do the following
#include <stdio.h>

int main()

{
int i=0;
do
{
printf("%d hello\n",i);


}

while(i<=5);

i=i+1;

the program enters in a kind of infinite loop
Why is that?
thank you!
 
Joined
Nov 23, 2022
Messages
7
Reaction score
1
Greetings guys,
While doing do while exercice

#include <stdio.h>

int main()

{
int i=0;
do
{
printf("%d hello\n",i);
i=i+1;

}

while(i<=5);



it works well but when I do the following
#include <stdio.h>

int main()

{
int i=0;
do
{
printf("%d hello\n",i);


}

while(i<=5);

i=i+1;

the program enters in a kind of infinite loop
Why is that?
thank you!

That's because you are not incrementing your counter i in the body of the loop. I is always a 0 and less then 5.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,146
Members
48,775
Latest member
satman49

Latest Threads

Top