Skip to main content
Ala
Senior
Sep 7, 2020 4:11 AM UTC
Solved

why it does not enter the if statement?

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.
    YAY.1
    YAY.1Best answer
    Senior
    Sep 7, 2020 6:30 AM UTC

    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
    Sep 7, 2020 5:51 AM UTC

    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
    Sep 7, 2020 6:07 AM UTC

    so what's the right way to do it?

    YAY.1
    YAY.1Best answer
    Senior
    Sep 7, 2020 6:30 AM UTC

    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
    Sep 7, 2020 7:00 AM UTC

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