FANDOM


Note: It is assumed you have read about the if statement before starting this tutorial.

Let's say you have an if statement and you want to execute something if the condition inside the if statement is false. That's where the else part comes in.

if( self.health > 75 )
{
    // if health is more then 75 do this...
}
else
{
    // whatever the first condition doesn't cover...
    // in this case, else would be if health is less then 75...
}

"Else" basically acts like a "default" condition, where if nothing is true then you do whatever if the default. You can use else when you want something to execute that isn't covered by the first if statement, but let's say you want more then one condition but you don't want a bunch of things running at once. You can then use else-if.


if( self fragButtonPressed() )
{
    // if the frag button is pressed do this
}
else if( self useButtonPressed() )
{
    // if the frag button isn't pressed but the use button is
}
else
{
    // if any other button is pressed not covered by the first two conditions
}

You use else when you want something to run when none of the conditions are met, and you use else-if when you want something to run if the condition before it isn't true, but you want to check if another is. You can have multiple else-if statements, but you can have only one else.