A quickie on if's or elses

Hi all, just a quick question while I’m finishing off (finally) my text 101 game…yes I’m still working on it from before the update. I’ve been a bit slow :slight_smile:
Anyway, my question is -
Inside one of my states, i need to check the value of variable, and the text changes depending on the value. There can be only 2 values on this variable, 0 or 1.
Is there a preferred method of dealing with this or does it not really matter.
Do i do this -

if (value==0) {
//do this stuff
}
if (value==1) {
//do this instead
}

Or, should i do something like this -

if (value==0) {
//do this stuff
}
else (value==1) {
//do this instead

Does it even matter?

Hi Robert,

If there can be only two possibilities then you could have this;

if(value == 1)
{
    // do stuff
}
else
{
    // do other stuff
}

The code examples you’ve gave in your post, the first could let both statements within the if statement execute if the value of value changed at any point in between, as unlikely as it may be, if could happen.

The second is safer, although you are missing an if in the syntax;

if (value == 0)
{
    // ...
}
else if (value == 1)
{
    // ...
}

But as you have already stated there can only be two possibilities you don’t really need to have the other condition stated at all, because if it isn’t one it is the other.

Not sure what data type you are using, but 0/1, on/off, true/false, that’s a boolean. So, if you defined your variable as such;

private bool hasFacialHair;

private void DisplayShavingStatus()
{
    if(hasFacialHair)
    {
        Debug.Log("I need a shave!");
    }
    else
    {
        Debug.Log("Smooth as the day I was born!");
    }
}

In the above, hasFacialHair is only going to equate to true if it has been set to true (the same as being set to 1, or on etc etc), so you can reduce the syntax a little further.

If you ever needed to have multiple values then the boolean isn’t going to be your friend, and in that case you may also use a switch statement instead;

private int hairsOnHead;

private void DisplayBaldnessStatus()
{
    switch(hairsOnHead)
    {
        case 0:
            Debug.Log("Oh dear.  Oh dear, oh dear!");
            break;
        case 1: 
            Debug.Log("One!  Shave that bad boy off right now!");
            break;
        case 2: 
            Debug.Log("Two!  A least they have each other!");
            break;
        default:
            Debug.Log("So dashing!  You are quite literally the modern day Prince Charming!");
            break; 
    }
}

I’m not sure why I went down the hair route in this example, nostalgia perhaps, but hopefully the underlying point I was trying to make, makes sense.

Hope this helps :slight_smile:

1 Like
if (value==0) {
//do Action A
}
else  {
//do Action B
}

In this case, Action A will happen or Action B will happen, but not both.

if (value==0) {
//do Action A
}
if (value==1) {
//do Action B
}

Action A can happen. Action B can also happen. Or neither may happen. Each one is checked independently.

if (value==0) {
//do Action A
}
else if (value==1) {
//do Action B
}

Action A will happen or Action B will happen (not both), but neither can happen.

1 Like

Privacy & Terms