user1681664
user1681664

Reputation: 1811

If statement skipping condition

I have some code, but the if condition is skipping and not being executed even though when it's true. My code in simple terms is as follows:

for(int i = 13;i<anarray.length;i++)
    if(i == 13)
    {
      for(w = i;w>12;w--)
      {
        if(anarray[w] > 0)     //the program skips this line completely even though the element is greater than 0
        { 
            //do some adding
        }
        if(anarray[w] < 0)
        {
            //do some other adding
        }
      }
    }

The following pictures should help:

Debug 1: enter image description here

Debug 2: enter image description here

Debug 3: enter image description here

Debug 4: enter image description here

Upvotes: 0

Views: 2295

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

Your problem is from

for(int w= m_RSISSteps - 1; w > m_RSISteps - 1; w--)

you just defined w to be equal to m_RSISteps - 1 so the < check evaluates to false and the for loop never executes. Likely your check needs to be corrected, perhaps you meant to do w >= 0 or use some other variable than m_RSISteps.

To turn it in to your "simplified example" it is like you did

for(int i = 12;i<anarray.length;i++) //These should be 12 not 13 based off of your images.
    if(i == 12)
    {
      for(w = 12;w>12;w--) //HERE
      {
        if(anarray[w] > 0)     //the program skips this line completely even though the element is greater than 0
        { 
            //do some adding
        }
        if(anarray[w] < 0)
        {
            //do some other adding
        }
      }
    }

Upvotes: 2

Related Questions