# Making Decisions In C++

Making decisions in C++ require the developer to specify one or more conditions to be evaluated by the program.

### Introduction

This is one of the core topics in programming and something you will use a lot. Every language I can think of has methods to make decisions. These are conditional statements and enable branching behavior.

Making decisions specifically means that you will be comparing data or C++ statements. We can do this with numeric or character operators. Logic operators work left to right. Now we can ask for input and make a decision.

### [If] Statement

Using [if] lets you make decisions in your statements. They will only execute [if] we meet a condition. That is how [if] works. For example, if one condition is true, your code can jump to a certain part and execute something. If the condition is false, it will go in another direction.

The syntax of an "if" statement looks like this:

if (condition){Do something based on condition}

The purpose of an "if" statement is to evaluate a condition. You can test if something is true or if it is false. An example is asking if the temperature is over 100 or less than 0. Then, in your block between the braces, the program does something based off the condition.

### Detriments Of Conditionals

We store all the conditionals that you write in memory. This means our program will have to access different portions of memory. This can slow a program down. I advise not to use a lot of conditionals. It's ok to use them in small numbers, just watch your performance.

### Making Decisions With An If Statement

#include <iostream>int main(){// variablesfloat gpa;// get inputstd::cout << "Please enter your gpa" << std::endl;std::cin >> gpa;// conditionalif (gpa>=3.0){  std::cout << "You have good grades, nice going!" << std::endl;}  return 0;}

This is our first example. Remember, the lines starting with "//" are for comments. They just document the code.

So, in this program, I wanted to ask someone's gpa. Then I wanted to congratulate them if it was good.

• created variable
• got input from user
• output a result

Look and see how the "if" statement was used there. Give it a condition and then do something.

You can see from the code that my condition was ">=3.0". When I ran this code, I inputted 3.2. I have high hopes for myself, as you can see! Anyway, because the condition was greater than or equal to 3.0, it output the congratulatory message.

What happens if my gpa is below 3.0? What will my code do? Since there is not a path for the program to take if the condition is false, it will do something useless. We need to add a condition that accounts for a lower than 3.0 gpa. Then, we need to tell the program what it should do at that point.

#include <iostream>int main(){// variablesfloat gpa;// get inputstd::cout << "Please enter your gpa" << std::endl;std::cin >> gpa;// if conditional is trueif (gpa>=3.0){  std::cout << "You have good grades, nice going!" << std::endl;}// if conditional is falseelse    std::cout << "You need to work harder" << std::endl;  return 0;}

In this snipet, I added an "else" condition. This tells the program what to do if the original conditional is false. So, if our gpa is below 3.0, then the program tells us to work harder. I also color-coded the changes I made so it would be easy to see.

With "if" statements, you can use them anywhere in your program. There can be multiple conditions and do multiple things. We can also test conditions as strings or other variables. Here is another example.

#include <iostream>int main(){// variablesstd::string fav_pokemon;// get inputstd::cout << " Try to guess my favorite Pokemon " << std::endl;std::cin >> fav_pokemon;// conditionalif (fav_pokemon == "darkrai")  std::cout << " You guessed right! " << std::endl;else  std::cout << " Sorry, that is incorrect " << std::endl;return 0;}

In this program, I am asking you to guess my favorite pokemon. Easy enough, right! As you can see, I am using strings this time to get string input. This is all very simple, but it shows you how you can use "if"statements.

Also know that we can test for multiple conditions. If I want to make it a bit easier to guess my favorite pokemon, we can add that to the test.

#include <iostream>int main(){// variablesstd::string fav_pokemon;// get inputstd::cout << " Try to guess my favorite Pokemon " << std::endl;std::cin >> fav_pokemon;// conditionalif (fav_pokemon == "darkrai" || fav_pokemon == "vileplume")  std::cout << " You guessed right! " << std::endl;else  std::cout << " Sorry, that is incorrect " << std::endl;return 0;}

As you can see above, I tested for either darkrai or vileplume to be true. So, if a person guessed either, then they got my true message.

### Complex Branching

Previously, we have just used a simple form of flow control. One"if" and one "else" statement. We have more options, however. There can be multiple branches. We call these nested statements.

Indent each level of nesting. This makes your code easier to read and prevents silly mistakes. Try to keep all the levels the same. You can use the "tab" key or whatever you want.

Be careful designing your "if else" statements. It is easy to get your program where it will compile, but you get the wrong output. Make sure and use braces to control the flow of your program. You don't want the compiler to give a wrong value through a logic error. These can be hard to track down because there are no warnings.

The compiler pairs an "else" with the nearest "if" that is not already paired. This is the purpose of braces, so you can make sure the program goes in the direction it is supposed to.

#include <iostream>int main(){// variablesfloat gpa;// get inputstd::cout << "Please enter your gpa" << std::endl;std::cin >> gpa;// conditionalif (gpa >= 3.8)  std::cout << " Those are exceptional grades " << std::endl;else if (gpa >= 3.0)  std::cout << "You have good grades, nice going!" << std::endl;else  std::cout << "You need to work harder" << std::endl;return 0;}

In the new section, I added another condition, by using an "else if" statement. This technique can give the beginner precise control of the flow of their program. It is testing for a different positive condition.

Now, if the gpa is between 3.0 and 3.8, it triggers a different message that is more accurate. If none of the conditions hold true, then our back statement at the end will display.

In this next example, I do the same with my pokemon guessing program.

#include <iostream>int main(){// variablesstd::string fav_pokemon;// get inputstd::cout << " Try to guess my favorite Pokemon " << std::endl;std::cin >> fav_pokemon;// conditionalif (fav_pokemon == "darkrai")  std::cout << " You guessed right! " << std::endl;else if (fav_pokemon == "vileplume")  std::cout << " I like that pokemon too ";else  std::cout << " Sorry, that is incorrect " << std::endl;return 0;}

Instead of using an "or" or "and" logic in my conditional statement, I break it up and use an "else if". This is just another way of doing it. Remember, we check conditions from the top. As soon as a condition is true, the rest of the block is skipped.

Keep in mind that any statement outside of the "if" block will always run. This is because it is not part of the conditions, so it is not checked and it is a regular C++ statement.

### Conclusion

C++ is a really cool language. Despite its age, it is still being updated and is one of the top languages around. For hardware programming it is still king and nothing will take its place soon. You can do much more than hardware programming in it, though.

Thank you for reading this, I really appreciate it.

If you would like to join my newsletter, you can do so here: