Problem with infinite loop in c program

Click For Summary
SUMMARY

The discussion centers on a C program that enters an infinite loop due to a common coding error. The specific issue arises from the use of a single equals sign in the condition else if (j=(n/2)+1), which causes assignment instead of comparison. This mistake leads to unintended behavior, as the loop continues indefinitely. To resolve this, the condition should be corrected to else if (j==(n/2)+1), ensuring proper comparison and allowing the loop to function as intended.

PREREQUISITES
  • Understanding of C programming syntax and control structures
  • Familiarity with conditional statements and comparison operators
  • Knowledge of debugging techniques in C
  • Experience with compiling C code and interpreting compiler warnings
NEXT STEPS
  • Review C programming best practices for conditional statements
  • Learn how to enable and interpret compiler warnings in GCC
  • Explore debugging tools for C, such as GDB
  • Study common pitfalls in C programming, focusing on assignment vs. comparison
USEFUL FOR

C programmers, software developers, and students learning about control flow and debugging in C. This discussion is particularly beneficial for those encountering similar infinite loop issues in their code.

colt
Messages
20
Reaction score
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
What are you trying to do?

This code is very very difficult to read.
 
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   Reactions: 1 person

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K