This site requires JavaScript, please enable it in your browser!
Greenfoot back
davemib123
davemib123 wrote ...

2013/7/2

Which is better: if, if or if else if

davemib123 davemib123

2013/7/2

#
Hi all, very quick query ... which is better to use? If statement, followed by another if statement or if statement followed by if else. I tried it out with my animation and both appear to do the same thing. What are your views? Example 1: just if statements
public void animateCoin()
    {
        if(AnimationCounter <=8 )
        {
            setImage(Coin1);
            AnimationCounter++;
        }
        if(AnimationCounter > 9 && AnimationCounter <=20 )
        {
            setImage(Coin2);
            AnimationCounter++;
        }
        if(AnimationCounter > 21 && AnimationCounter <=32 )
        {
            setImage(Coin3);
            AnimationCounter++;
        }
         if(AnimationCounter > 33 && AnimationCounter <=44 )
        {
            setImage(Coin2);
            AnimationCounter++;
        }
         if(AnimationCounter > 45 && AnimationCounter <=56 )
        {
            setImage(Coin1);
            AnimationCounter++;
        }
        if(AnimationCounter >= 57)
        {
            AnimationCounter = 0;
        }
    }
Example 2: if, else if
public void animateCoin()
    {
        if(AnimationCounter <=8 )
        {
            setImage(Coin1);
            AnimationCounter++;
        }
        else if(AnimationCounter > 9 && AnimationCounter <=20 )
        {
            setImage(Coin2);
            AnimationCounter++;
        }
        else if(AnimationCounter > 21 && AnimationCounter <=32 )
        {
            setImage(Coin3);
            AnimationCounter++;
        }
        else if(AnimationCounter > 33 && AnimationCounter <=44 )
        {
            setImage(Coin2);
            AnimationCounter++;
        }
        else if(AnimationCounter > 45 && AnimationCounter <=56 )
        {
            setImage(Coin1);
            AnimationCounter++;
        }
        else if(AnimationCounter >= 57)
        {
            AnimationCounter = 0;
        }
    }
davmac davmac

2013/7/2

#
I think "else if" makes the intention clearer, so it is a bit easier to understand. In some cases (not so much the example you have here) it is also more efficient, since the subsequent conditions only have to be evaluated if the first one isn't true.
Kartoffelbrot Kartoffelbrot

2013/7/2

#
And if you use else, you won't have bugs sometimes, like this:
int num = 1;
if(num==1)
num++;
if(num==2)
num++;
if(num==3)
num++;
if(num==4)
num=1;
It isn't a nice example, but it shows, that without else you num will be 1 at the end, although you wanted it to be 2.
davemib123 davemib123

2013/7/2

#
well i'm still learning, so I wanted some advice :). Many thanks guys!
Kartoffelbrot Kartoffelbrot

2013/7/2

#
I am still learning, too. :)
danpost danpost

2013/7/2

#
As far as doing the same thing, they do not. Because you are changing the value of the AnimationCounter within the 'if' blocks, in Example 2, the value will be 57 from one act cycle to the next; while in Example 1, its value is first incremented to 57, then changed back to zero during the same act and will not be 57 between acts. However, in both cases, it will never reach 57 as when its value is 9 no case is valid and its value will never increase. The same goes with 21, 33, and 45.
danpost danpost

2013/7/2

#
You can also simplify things a bit; (1) since you are incrementing the counter in every case, you should probably do that after all the 'if's (or 'if else's); and (2) by using just the upper limits in the conditions, you can eliminate having to check both (while using 'if else').
public void animateCoin()
{
    if(AnimationCounter < 9 )
    {
        setImage(Coin1);
    }
    else if(AnimationCounter < 21 )
    {
        setImage(Coin2);
    }
    else if(AnimationCounter < 33 )
    {
        setImage(Coin3);
    }
    else if(AnimationCounter < 45 )
    {
        setImage(Coin2);
    }
    else // must be 45 or more
    {
        setImage(Coin1);
    }
    AnimationCounter++;
    if(AnimationCounter == 57)
    {
        AnimationCounter = 0;
    }
}
Even better might be to use a 'switch' statement (which will avoid setting the image EVERY act method) and the running of the counter can be reduced to one statement:
public void animateCoin()
{
    switch(AnimationCounter)
    {
        case 0: 
        case 45: setImage(Coin1); break;
        case 9: 
        case 33: setImage(Coin2); break;
        case 21: setImage(Coin3); break;
    }
    AnimationCounter = (AnimationCounter + 1) % 57;
}
davemib123 davemib123

2013/7/2

#
Thanks for the info Danpost, much appreciated :) If you don't mind could you explain this in layman terms:
AnimationCounter = (AnimationCounter + 1) % 57;  
danpost danpost

2013/7/2

#
Refer to the Java tutorial page on Arithmetic Operators.
You need to login to post a reply.