Something about programming

Conditions in C++: if... else if... else and switch

Previous tutorial: Operators in C++

Let's imagine a situation in our game: some character under AI need to decide what to do, it can fight player or it can flee and it depends on character's health. Let's create a variable that will control enemy health:

int enemyHealth = 100;

Now we need to decide what to do. For this, we can use conditions in C++.

if Statement in C++

For now let's check if health is above 50, then the enemy will attack the player (player health will be decreased):

int enemyHealth = 100; int playerHealth = 100; if (enemyHealth > 50) { playerHealth -= 10; }

if statement consists of keyword if, then condition in parenthesis and then we put in curly braces the code that will be executed if the condition is true. The condition is always resolved to some bool value - it can be true or false.

So if enemy health is above 50, enemy attacks player - we decrease player's health. But nothing happens if health is less than 50. Let's fix it

else

if statement can have additional blocks. One of them: else. It's executed if condition of all previous blocks is false:

int enemyHealth = 100; int playerHealth = 100; if (enemyHealth > 50) { playerHealth -= 10; } else { enemyHealth += 10; }

If enemy health is below 50 (condition is false), then block else is executed. And in this case we increase enemy health (let's imagine that he's found medkit).

It's a very simple case, we know that health is always 100 and we can write right below variable initialization what to do. But when our program will become larger we'll no longer be able to say what the health is at the current time. When we'll learn how to work with functions in C++, we'll be able to build much more interesting examples.

else if

if can have several blocks else if. We use them if we need additional conditions:

if (enemyHealth > 80) { playerHealth -= 10; } else if (enemyHealth > 50 { // some action1 } else if (enemyHelath > 20) { // some action2 } else { enemyHealth += 10; }

Full condition looks like this: if health is above 80 we decrease player's health. If it's not true (health is less than 80), we check next block: if health is above 50. If it's true (health is between 50 and 80) we execute code action1. If it's false, we check the next condition: if health is above 20. If all conditions are false, block else is executed. It will be executed only if health is less than 20.

This whole code belongs to one if and only one block will be executed.

Switch Statement

C++ language has another conditional statement - switch. It's used in situations when we know what values condition can take. For example let's read user input and check what he printed:

char i; cin >> i; switch (i) { case 'i': cout << "Open inventory" << endl; break; case 'm': cout << "Open map" << endl; break; default: cout << "Wrong key was pressed" << endl; }

First, we put keyword switch. Then, variable that we'll check in parenthesis. After that, we put the body of the switch in curly braces. The body of switch consists of one or more cases. In each case, we check the specific value of the variable. Pay attention that we put keyword break in each case. It tells to skip all next cases. There is also default case - it's executed if all cases above it are false.

Conclusion

Conditions is the core of any programming language, including C++. C++ provides two different conditional statements: if...else if...else and switch. We can use only if but switch is more convenient sometimes.

Comments:

No comments yet