Conditional Logic and if() statements

Conditional Logic

When a game is running, often there are lines of code that are are constantly happening. The game’s engine will constantly be, for example, calculating physics, drawing an image on the screen, etc.

However, there are many instances where code will only run when certain conditions are met. For example, when you press a button in a game, some series of actions will occur.

Broadly speaking, we can describe certain things happening only when certain conditions are met as conditional logic. It is the decision making process at the heart of, amongst many other things, game design. We know this is particularly important for game design, as the key facet of play is interactivity.

if() statements

if() statements are the foundation of conditional logic. They are made up of two parts; conditions and actions.

Conditions are the properties within a game that must be true in order for some code to be run. It could be when the player runs out of ammo, when they are touching a collectible, or when they press a certain button.

Conditions must return true for the execution to occur. This might just be a boolean variable that you are using in your game, or you might have to make some kind of equality check on some variable. Below are some examples that you might need to use:

== check for equality, as in if(health == 0)

!= check for inequality (not equal), e.g. if (playerIsGrounded != true)

> check for greater than, e.g. if (distance > 10)

< check for less than, e.g. if(health < 1)

>= check for greater or equal to

<= check for less than or equal to

If you were checking for equality of a boolean (a true or false), we can write if(playerIsGrounded == true) simply as if(playerIsGrounded).

You may also want to check multiple conditions at the same time, and run code if two (or more conditions are true, or maybe if one condition or another condition are true. We can do this by using the following symbols inside the if() condition:

&& both statements are true, e.g. AND

|| one or more of the statements is true, e.g. OR

^ only one of the statements is true, e.g. XOR

if (hungry && haveMoney) 
{
    //Buy some food if both statements are true.
}

if (hungry || haveMoney) 
{
    //Buy some food if at least one of the statements are true.
}

if (hungry ^ haveMoney) 
{
    //Buy some food if one of the statements are true, but not both.
}

Here, I am using // to just describe what the action could be. Two forward-slashes in front of a line of code turns it into a comment, a line of code that is ignored by the compiler and is just for the programmer to read.

Sometimes, we may check some condition and run code if it's true, but also run some specific code if it's not true. To do this, we need to add an else statement after the if() statement.

if (hungry)
{
    //buy some food.
}

else
{
    //buy some video games.
}

We can chain these together to make the else-if statement:

if (hungry && money > 10)
{
    //buy some food.
}

else if (money > 100)
{
    //buy some video games.
}

else
{
    //Go home sad.
}

Actions within an if() statement are the lines of code that run only when the if() statement’s condition returns true. The actions might be changing a variable, loading a new scene, spawning another object, etc. We can also run if() statements within other if() statements, a.k.a nesting:

if (money > 10)
{
    //Buy some food.
    
    if (money > 100)
    {
        //Buy some video games.
    }
}

Each execution requires its own semicolon at the end of it (;) as each is its own discrete command. For example then, we might have an if() statement that checks whether the shoot button is pressed in an FPS game. More than one thing would happen when the shoot button is pressed, and each would have their own semicolon:

{
    ammo--;                 //Reduce the ammo counter by 1.
    Audio.Play(“Shoot”);    //Play some sound.
    Instantiate(bullet);    //Spawn a bullet.
}

Also note that all of the actions above are encapsulated by some braces ( squiggly brackets { } ). We don’t have to do this if we only have one action in our if() statement, but if we have more than one, we need to encapsulate them together.

Here’s an example of an if() statement with just one action:

if (mouseButtonPressed) ammo--; 

However, if we have more than one action, we have to encapsulate them in braces:

if (mouseButtonPressed) 
{
    ammo--;
    Audio.Play(“Shoot”);
} 

Because it is there is no downside to encapsulating just one action, many programmers just do it anyway:

if (mouseButtonPressed) 
{
    ammo--;
}

Last updated