# Problem with infinite loop in c program

Hi, I have a program that is entering a infinite loop in the last if else of this loop. The program is printing 3 endless times. Here is the code that generates it:
Code:
 else {
for (j=1;j<n;j++) {
if (j<=(n/2)) {
a[j] = j-1;
printf ("%d", a[j]);
}
else if (j=(n/2)+1) {
a[j] = a[j-1];
printf ("%d", a[j]);
//printf ("%d", j);
//break;
}
else if  {

a[j] = a[j-1]-1;
printf ("%d", a[j]);
break;
}
}

The three should be printed twice in a row, once by the if and the second time by the middle else if. Then the else should print decreasing values until the for loop end.
So, I don't understand why this is happening . J value it is 5 when it leaves the middle else if, which it is the correct value. Then it should not enter the last "else" in this iteration, since j needs to be bigger than (n/2)+1, which I expect to be (9/2)+1, which should be rounded to 5, so j should enter the last else only in the next iteration when it's value should be 6 and keep entering it for two more iterations, until it reaches the value of 9, which should end the for loop.

Here is the link for the full code in the case someone can compile it:

Thanks for any input.

DavidSnider
Gold Member
What are you trying to do?

This code is very very difficult to read.

D H
Staff Emeritus
• 