简体   繁体   中英

IF statement comparing

In my IF statement, its kinda weird, cause every time my code loops in it, only SLOT1 is being called and displayed in my debugger, Why is it? Is there something wrong with my statement? Thanks.

    choiceSlot1 = TRUE;
    choiceSlot2 = TRUE;
    choiceSlot3 = TRUE;
    choiceSlot4 = TRUE;   

    if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1 ) {

        NSLog(@"Slot1");
        choiceSlot1 = FALSE;

    } 
    else if (slot2 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot2 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot2) {

        NSLog(@"Slot2");
        choiceSlot2 = FALSE;


    }
    else if (slot3 != [carousel1 indexOfItemViewOrSubview:carousel1.superview] || twoSlot3 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot3) {

        NSLog(@"Slot3");
        choiceSlot3 = FALSE;


    }
    else if (slot4 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot4 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot4) {

        NSLog(@"Slot4");
        choiceSlot4 = FALSE;

    }

Because the else if is only validated in case the first if statement is false .

Example:

if (true) 
{
    // Will be executed
} 
else if (true) 
{
    // Will not be executed
}

if (false) 
{
    // Will not be executed
} 
else if (true) 
{
    // Will be executed
}

It's easy,

 if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1 ) 

If choiceSlot1 is TRUE, then the first statement is always going to be true (with an OR, its true if one of the elements is true).

When the first statement is true, then the rest of the else/if are not called!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM