How Are If Statements Used In Game Dev?

Thinking about getting into programming and wondering what you need to know? Maybe you’ve heard some terms like if statements, for loops, functions, or variables and you’re curious about what they are! In this article, I will be talking about if statements and how they are used in game development.

An if statement is a conditional block of logic used to check if the given expression is true or not. They are used in game development to check collisions, input, game rules, level boundaries, or any aspect that requires a check to validate if the code should proceed.

If statements are fundamental building blocks to coding, along with for/while loops, functions (methods), variables, etc.

You can think of an if statement like a light switch in your house or turning your phone on or off.

  • If the light switch is up, turn on the light, else turn it off.
  • If your phone is off and the power button is pressed, turn on the phone. If the phone is on and the power button is pressed for 3 seconds, turn off the phone.

Are If Statements Used In Game Dev?

If statements are used in game development. They are a fundamental piece of logic used in developing almost all software. If statements are used to perform a block of code if the condition has been met. They can be used to check input from a controller, if the game character has collided with an object – the possibilities are endless.

Example of an if statement in Unity checking if the space key was pressed on the keyboard.

What Are If Statements

If statements, also known as if/else statements, are conditional logic blocks that allow something to either happen or not based on certain information or parameters. For example: If the player has the red key card, the play can open the red door.

If statements allow an event to occur (or not) based on a condition and can have multiple conditions in one statement. Additionally, if the first conditional statement is not met a second, third, fourth, etc. can be made afterward.

// EXAMPLE IF STATEMENT USAGE.
if (playerHasRedKey == true) {
  // do something, like open red key card door.
}
else {
  // Notify player they require the red key card.
}

How Do If Statements Work?

If statements work by checking if the provided conditional statements pass the expected outcome. If all the parameters pass the requirements in the if statement the code within the statement will execute. Once a condition has been met in an if statement, the other conditions are ignored.

Each programming language will have its own syntax that is used to code an if statement. Below is an example of an if statement, one using Python and one using C++.

# python
a = True
b = False

if a == True and b == True:
  print("both a and b are true")
elif a == True:
  print("a is true")
elif a == b:
  print("b is true")
else:
  print("neither a or b are true")
// C++
#include <iostream>

bool a = false;
bool b = true;

if (a == true && b == true) {
  std::cout << "both a and b are true";
}
else if (a == true) {
  std::cout << "a is true";
}
else if (b == true) {
  std::cout << "b is true";
}
else {
  std::cout << "neither a or b are true";
}

Notice the difference between the two? From how the variables are defined to how the if statements are set up, both are very different but both do the same function. Is one programming language better than the other? No! Each programming language serves its own purpose.

The order of the if statements matter too. In the example below I have a correct way of setting up the if statement and an incorrect way. Notice in the incorrect way I have the check for testing if a and b are true last.

This statement will never trigger as the if statement will go in order of the conditional statements.

In the correct example, (a == true && b == true) will be checked first, then (a == true), then (b == true), finally the else will be hit.

In the incorrect example, (a == true) will be checked first, next (b == true), then (a == true && b == true), finally the else will be hit.

In the correct way when a and b are both true the first if statement is checking if they are both true. In the incorrect version, we’re checking if a is true, which logically speaking is true. So we go inside that code block and ignore the other if statements, since only the first condition that is met is used.

// Correct way

if (a == true && b == true) {
  std::cout << "both a and b are true";
}
else if (a == true) {
  std::cout << "a is true";
}
else if (b == true) {
  std::cout << "b is true";
}
else {
  std::cout << "neither a or b are true";
}
// Incorrect way

if (a == true) {
  std::cout << "a is true";
}
else if (b == true) {
  std::cout << "b is true";
}
else if (a == true && b == true) {
  std::cout << "both a and b are true";
}
else {
  std::cout << "neither a or b are true";
}

What Is The Difference Between If Statements And Switch Cases?

When an if statement condition is not true, then the else if/else block will be executed. In switch statements when a value does not match any case then the default statement is executed (if provided). If statements can use multiple conditions to determine the outcome, whereas a switch statement can only use a single expression.

Below is a demostration of how an if statement and swtich statement would evaulate the same damage type.

As we can see the if satatement will evaluate sequencetally, where the swtich statement evaulates based on if it matches a case.

Summary

If statements are fundamental programming blocks in game development and software development in general. They are used in game development for checking when a user input has been made if the input was pushed, held, or released. Along with checking game rules and logic, or any other aspect in a game where the programmer may want something to occur or not occur.

Example: If Mincraft player clicks bed at night time, player sleeps in bed. Else, tell player they need ot wait until night time.

The examples are endless.


Sources

Java Point: if/else vs switch

IBIBLIO: if statement

Tech Differences: Difference between if/else and switch