Skip to main content
Ala
Senior
September 7, 2020
Solved

why it does not enter the if statement?

  • September 7, 2020
  • 4 replies
  • 2422 views

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?

    This topic has been closed for replies.
    Best answer by YAY.1

    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.

    4 replies

    Tesla DeLorean
    Guru
    September 7, 2020

    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Ala
    AlaAuthor
    Senior
    September 7, 2020

    so what's the right way to do it?

    YAY.1
    YAY.1Best answer
    Senior
    September 7, 2020

    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.

    Ala
    AlaAuthor
    Senior
    September 7, 2020

    thank you all guys! you're amazing!:)