cancel
Showing results for 
Search instead for 
Did you mean: 

why it does not enter the if statement?

Ala
Senior

I have a code made to check an if statement and then perform something. here is the if statement

for(int j=0;j<10;j++)
{
   if(removed_Num==USERS[j])
   { //do something}
}

USERS[j] is a 13 element array and removed_Num is also a 13 element array. I've check in the debug mode and saw USERS[j] is equal with removed_Num. but it doesn't enter the if statement. why is that?

1 ACCEPTED SOLUTION

Accepted Solutions
YAY.1
Senior

Clive1 is right.

If you compare two array with all indexes, you need select index one by one and compare them.

I wrote a example,

  1. Int Issamearray;
  2. for(int j=0;j<10;j++)
  3. {
  4. Issamearray = false;
  5. if(removed_Num[j] ==USERS[j])
  6. { Issamearray = True;}
  7. }
  8. If (Issamearray == True)
  9. {
  10. //do something}
  11. } ​

​Not; if you make this with while not for, it can will work faster.

I hope you understand clearly.

View solution in original post

4 REPLIES 4

As described that isn't how C works.

The comparison here is of the address of an array vs the element in a second array. You basically ask it to compare apples and oranges, which usually they don't.​

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Ala
Senior

so what's the right way to do it?

YAY.1
Senior

Clive1 is right.

If you compare two array with all indexes, you need select index one by one and compare them.

I wrote a example,

  1. Int Issamearray;
  2. for(int j=0;j<10;j++)
  3. {
  4. Issamearray = false;
  5. if(removed_Num[j] ==USERS[j])
  6. { Issamearray = True;}
  7. }
  8. If (Issamearray == True)
  9. {
  10. //do something}
  11. } ​

​Not; if you make this with while not for, it can will work faster.

I hope you understand clearly.

thank you all guys! you're amazing!🙂