Problem with infinite loop in c program

In summary, the conversation discusses a program that is entering an infinite loop in the last if else statement. The code provided shows a potential bug where the single equal sign is used instead of a double equal sign, causing the program to assign a value to a variable instead of comparing values. The conversation also mentions the correct value that should be used in the if else statement and how the program should behave.
  • #1
colt
22
0
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[1][j] = j-1;
        printf ("%d", a[1][j]);
      }
      else if (j=(n/2)+1) {
        a[1][j] = a[1][j-1];
        printf ("%d", a[1][j]); 
        //printf ("%d", j);
        //break;
      }
      else if  {
         
        a[1][j] = a[1][j-1]-1;
        printf ("%d", a[1][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:
http://codepad.org/66MYRyRQ

Thanks for any input.
 
Technology news on Phys.org
  • #2
What are you trying to do?

This code is very very difficult to read.
 
  • #3
The code you entered is not the same as your code at codepad.org. The code you entered at this site isn't valid code. However, the bug is still present. Here it is:

else if (j=(n/2)+1) ...

You used a single '=' sign rather than two. With two you are comparing but with one you are assigning. You are assigning a value to j, and hence the infinite loop.

A good compiler will tell you about this common mistake if you let it do so. Enable all common warnings when compiling and then pay attention to those warnings.
 
  • Like
Likes 1 person

1. What is an infinite loop in a C program?

An infinite loop is a programming construct in which a set of instructions or statements will continue to be executed repeatedly without ever terminating, creating a loop that runs infinitely.

2. How does an infinite loop occur in a C program?

An infinite loop occurs when the condition or statement that controls the loop never evaluates to false, causing the loop to continue running indefinitely.

3. What are the consequences of an infinite loop in a C program?

The consequences of an infinite loop can be severe, as it can cause the program to crash or freeze, consume a large amount of memory, and prevent the program from performing any further tasks.

4. How can you fix an infinite loop in a C program?

To fix an infinite loop, you need to identify the cause of the loop and make sure that the condition or statement controlling the loop eventually evaluates to false, allowing the loop to terminate.

5. What are some strategies for preventing infinite loops in C programs?

Some strategies for preventing infinite loops include using a counter variable to limit the number of loop iterations, using a break statement to exit the loop under certain conditions, and thoroughly testing the program before running it to catch any potential infinite loops.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
607
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top