Learn To Code In C++ For Unreal Engine – Conditional Statements

Table of Contents

Learn To Code In C++ For Unreal Engine – Conditional Statements

Reading Time: 4 minutes
Level: Beginner
Version: Visual Studio 2019 Community Edition

Help Others Learn Game Development

In game development and programming in general, you will stumble upon situations where you need to make a decision: pick an item or not, shoot the enemy or not, move left or right, and so on.

And based on the decision you make the gameplay workflow will continue to run. Well, these decisions are made using conditional statements.

The Anatomy Of A Conditional Statement

The most famous conditional statement is called the if statement, and it goes like this:

				
					if(condition)
{
    // code to execute if condition is true
}
				
			

The statement starts with if and inside the parenthesis we put the condition that we are trying to evaluate.

The condition needs to be true in order for us to execute the code that’s between {} 

This is where booleans come in. If you remember when we introduced booleans in the first post I mentioned that they are used for conditional statements because their value can either be true or false.

If we rewrite this line of code:

				
					if(true)
{
   std::cout << "This statement is true";
}
				
			

And if you run the app now you will see “This statement is true” printed in the console. 

We can test a more concrete example inside the if statement:

				
					if(3 > 2)
{
   std::cout << "3 is greater than 2";
}
				
			

Of course, the code above will always print “3 is greater than 2” because this example is hardcoded. 

Let’s see a real world example:

				
					int Health = 100;

if(Health < 0)
{
   std::cout << "Player died game over";
}
				
			

Here, we have a Health variable that we are testing if it’s lower than 0, and this is something that we will do a lot when we start creating our games.

If this is true we are printing “Player died game over” but in our game we will either show some UI to inform the game that the player has died, or we will simply restart the game or something else that makes sense.

One thing that you noticed so far we are comparing numbers to numbers. So far we performed two tests to see if the number is less than a number and if a number is greater than a number.

Here are all comparisons that we can test with numbers:

				
					// if a is GREATER THAN b
if (a > b)
{
}

// if a is GREATER THAN or equal to b
if (a >= b)
{
}

// if a is LESS THAN b
if (a < b)
{
}

// if a is LESS THAN or equal to b
if (a <= b)
{
}

// if a is equal to b
if (a == b)
{
}

				
			

These comparisons are the same ones we learned to perform in math. We can test if a number is greater than a number, greater than or equal to, less than number, less than or equal to, and if the number is equal to a number.

You will notice that we are using the double equal sign “==” to perform the equals test, this is because we use single equal sign “=” to assign a value to a variable, so we can write something like: 

				
					if (a = b)
{
}

				
			

This will not work. Instead, when you want to compare the two numbers if they are equal you use the double equal sign ==.

What If The Statement Is Not True?

So far we tested only for one condition using the if statement, but what happens if that condition is not true?

For example:

				
					int a = 3;
int b = 5;

if(a > b)
{
    std::cout << "a is greater than b";
}
				
			

In this case we are testing a which has a value of 3 if it’s greater than b which has a value of 5. Obviously this condition is not true because 3 is not greater than 5, so if we can’t execute the code inside the if statement, how can we control the flow of our game?

For this we can use the else statement:

				
					int a = 3;
int b = 5;

if(a > b)
{
    std::cout << "a is greater than b";
}
else
{
    std::cout << "a is NOT greater than b";
}
				
			

In this case, if the first condition of a > b is not true, then the code inside the if statement will not be executed, instead we will proceed to execute the code inside the else statement.

This is how we can have two options in our game that the player can chose, and based on his decision you will continue the game in different way, for example you can spawn 5 enemies that will attack the player instead of 10.

But this if else condition is also limited because it allows us to only test two conditions, maybe we have multiple choices that we can choose from. 

Well, there is a solution for that as well: 

				
					int a = 3;
int b = 5;

if(a > b)
{
    std::cout << "a is greater than b";
}
else if(b > a)
{
    std::cout << "b is greater than a";
}
else if(a == b)
{
    std::cout << "a is equal to b";
}
				
			

We can perform multiple condition tests by using else if statements, and based on the outcome we can execute specific lines of code.

Here is another more concrete example that we will encounter in our game development journey:

				
					int Mana = 10;

if(Mana >= 90)
{
    // perform special attack 1
}
else if(Mana >= 50)
{
    // perform special attack 2
}
else if(Mana >= 20)
{
    // perform special attack 3
}
else
{
    // perform special attack 4
}
				
			

Based on the Mana value and the condition that we are testing we are going to perform different attacks in our game.

This is one of the ways how you can give a power boost to your game character – the lower his health is, the better and stronger attack he can perform.

The AND & OR Operators

We can also test for multiple conditions inside the if statement:

				
					int Health = 50;

if(Health > 10 && Health < 60)
{
    // execute code
}
				
			

Inside the if statement above we are testing if the Health variable is greater than 10 AND if its less than 60.

In this case when we use the && (and) operator both conditions needs to be true in order for us to execute the code inside this if statement.

For example, if the Health value is 70, this if statement will not be true, because even though 70 is greater than 10, it is not less than 60, so we will not execute the code inside this if statement.

Or if the value of Health is 10, still it will not be true, because even though 10 is less than 60, it is not greater than 10, since 10 is equal to 10.

And you are not limited to testing only two conditions, you can test multiple conditions:

				
					if(Health > 10 && Health < 60 && EnemyIsAlive && Power > 50)
{
    // execute code
}
				
			

Besides testing if the Health is greater than 10 and less than 60, we are also testing if the EnenyIsAlive and if the Power is greater than 50, all these conditions needs to be true for the whole if statement to work.

If Health is greater than 10 and less than 60, and the enemy is alive(EnemyIsAlive == true), but Power is not greater than 50, then this whole if statement is not true.

We can also test if at least one of the conditions is true:

				
					if(Health > 10 || Power < 70)
{
    // execute code
}
				
			

Here we are testing if Health is less than 10 or if the Power is greater than 70. 

In this case if Health is 15, and Power is 71, the if statement will be true, because when we use the || (or) operator at least one of the conditions needs to be true for the whole if statement to work.

And same as with the && operator, you can also test multiple conditions:

				
					if(Health > 10 || Power < 70 || LevelTimer > 300 || EnemyCount < 20)
{
    // execute code
}
				
			

If at least one of these conditions is true then the whole if statement is true and the code inside will execute.

Switch And Case Statement

There is another conditional statement called switch and case that we can use to evaluate conditions:

				
					int Health = 5;

switch(Health)
{
    case 1:
        // execute code
        break;
    
    case 3:
        // execute code
        break;
    
    case 4:
        // execute code
        break;
    
    case 5:
        // execute code
        break;
}
				
			

When testing a condition with switch and case, you pass the variable you want to test to switch, and inside the body of switch you use cases to test for conditions.

Every case acts as a test for the value, so case 1 is testing if the Health variable has a value of 1, case 3 is testing if the value is equal to 3, and so on.

If one of the cases is true, then the code inside that case will execute and then it will hit the break statement and exit outside the switch and case.

This means that cases below will not be tested if one of the cases above evaluates to be true. 

For example:

				
					int Health = 5;

switch(Health)
{
    case 1:
        // Health Not EQUAL to 1
        break;
    
    case 5:
        // Health Equal to 5 
        // Code here will execute
        // Then the workflow will
        // exit outside the switch and case
        break;
    
    case 6:
        // This condition is not tested since
        // the case above evaluated to be true 
        break;
    
    case 8:
        // This condition is not tested since
        // the case above evaluated to be true
        break;
}
				
			

To explain how switch and case works, let us re-write the same condition using if else:

				
					int Health = 5;

if(Health == 1)
{

}
else if(Health == 3)
{

}
else if(Health == 4)
{

}
else if(Health == 5)
{

}
				
			

This is how we would test for the same condition above using if else statements.

So the purpose of both statements are to test for conditions, and you will sometimes use one over the other.

Every time I say this I automatically get a question “when should I use if and when should I use switch and case?”

And the answer is there is no general rule where to use if where to use switch and case, the feeling for that comes with practice and going through multiple examples.

That is the reason why I cover a lot of topics and create a lot of games in my tutorials, because when you see something in action, then you tend to understand it more.

But for now, what we covered here is enough because this is the basis for everything that will come when we start creating our games.

Where To Go From Here

In this post you learned how to use conditional statements to create choices in your games.
 
To continue your learning journey you can take a  look at our Introduction To Loops Lecture which is the next lecture in this tutorial series.

Leave a Comment