# Conditionals

In addition to using variables and data for math operations, we can use them to affect how our program is run. Conditionals allow us to perform certain code depending on whether or not some value is “true” or “false.”

### If Statements

In an “if statement” we can write code that should only be executed if a certain value or expression is “true.” Take a look at the code below and try to predict what it would print to the screen:

```#include <iostream>

int main() {
float motorSpeed = 2.5;
if (motorSpeed > 1) {
std::cout << "The motor speed is greater than 1.\n";
}
return 0;
}
```

To write an if statement, you simply type the word “if” followed by some “condition” you want to check, where the condition must either be true or false. In the case of the code above, the condition was `motorSpeed > `1. Since the `motorSpeed` (set to 2.5) is greater than 1, the code in the curly braces is run. In other words, the code in the curly braces is run because the condition `motorSpeed > 1` is true.

Similarly, if the condition in the parentheses were false (like below), the code in the curly braces would not be run:

```#include <iostream>

int main() {
float motorSpeed = 2.5;
if (motorSpeed < 1) {
std::cout << "The motor speed is less than 1.\n";
}
return 0;
}
```

### Else Statements

If we wanted to put the above two pieces of code together, we could use an “else statement.” The code inside of an else statement is only run if the condition inside an above if statement is false.

```#include <iostream>

int main() {
float motorSpeed = 2.5;
if (motorSpeed < 1) {
std::cout << "The motor speed is less than 1.\n";
}
else {
std::cout << "The motor speed is greater than or equal to 1.\n";
}
return 0;
}
```

Because `motorSpeed < 1` in the if statement is false, only the code inside the else statement is run in the code above.

### Else If Statements

We can go one step further and use “else if statements.” These execute if the condition of an above if or else if statement is false and the condition inside the else if statement’s parentheses is true.

```#include <iostream>

int main() {
float motorSpeed = 2.5;
if (motorSpeed < 1) {
std::cout << "The motor speed is less than 1.\n";
}
else if (motorSpeed < 2) {
std::cout << "The motor speed is less than 2 but greater than or equal to 1.\n";
}
else if (motorSpeed < 3) {
std::cout << "The motor speed is less than 3 but greater than or equal to 2.\n";
}
else {
std::cout << "The motor speed is greater than or equal to 3.\n";
}
return 0;
}
```

Note that code within the curly braces of any of these statements should be indented (although it’s not required). This makes the code much easier to read.

### Other Comparison Operators

In the above examples, we only used the less than symbol to compare two values, but we can compare values in other ways as well.

Note that two equals signs must be used to check if values are equal (as opposed to a single equals sign used for setting a variable equal to a value).

### Logical Operators

Logical operators can also be useful in if statements. With logical operators, we can check the following:

• If one condition and another condition is true
• If one condition or another condition is true
• If a condition is not true

Take a look at the table below to see how to use the different logical operators:

Try out the program below to see these logical operators in action:

```#include <iostream>

int main() {
float chanceOfRain = 0.7;
int temperature = 80;
bool isWindy = true;
bool isSunny = false;

if (chanceOfRain > 0.5 && !isSunny) {
std::cout << "You should take an umbrella.\n";
}
else {
std::cout << "You should not need an umbrella.\n";
}

if (temperature < 60) {
std::cout << "It is cold outside.\n";
}
else if (temperature < 70) {
if (isWindy || !isSunny) {
std::cout << "It is cool outside.\n";
}
else {
std::cout << "It is warm outside.\n";
}
}
else if (temperature < 90) {
if (isWindy || !isSunny) {
std::cout << "It is warm outside.\n";
}
else {
std::cout << "It is hot outside.\n";
}
}
else {
std::cout << "It is hot outside.\n";
}

return 0;
}
```

Note how you can also place if statements inside of other if statements (referred to as “nested” if statements).

### Some Shortcuts

##### Switch Statements

If you ever have code that looks like this…

```#include <iostream>

int main() {
int x = 5;
if (x == 0) {
std::cout << "A\n";
}
else if (x == 1) {
std::cout << "B\n";
}
else if (x == 4) {
std::cout << "C\n";
}
else if (x == 5) {
std::cout << "D\n";
}
else {
std::cout << "E\n";
}
return 0;
}
```

…then the “switch statement” can allow you to do the same thing, but with a little less typing. Here’s that same basic program structure, this time utilizing the switch statement:

```#include <iostream>

int main() {
int x = 5;
switch (x) {
case 0:
std::cout << "A\n";
break;
case 1:
std::cout << "B\n";
break;
case 4:
std::cout << "C\n";
break;
case 5:
std::cout << "D\n";
break;
default:
std::cout << "E\n";
}
return 0;
}
```

Basically, the switch statement allows you to take one variable and do different things depending on the possible values (each “case”) of that single variable. There are just a couple things to keep in mind:

• Typing “`break;`” at the end of each “case” section just means that you want to exit the switch statement. If this weren’t included, then every case (including default) would be checked even if a matching case had already been found. Try removing the “`break;`“s, and see what happens.
• You can add a “`default:`” case to your switch statement (as shown above), which is called for any value of the variable that is being used (unless a `break;` had been triggered beforehand).
##### The Ternary Operator

For a quick way of writing an if-else statement in a single line, the ternary operator can be very useful. Here’s some example code that can be shortened with the ternary operator:

```#include <iostream>

int main() {
int temperature = 85;
if (temperature > 80) {
std::cout << "It's hot.\n";
}
else {
std::cout << "It's not hot.\n";
}
return 0;
}
```

And here it is shortened with the ternary operator:

```#include <iostream>

int main() {
int temperature = 85;
temperature > 80 ? std::cout << "It's hot.\n" : std::cout << "It's not hot.\n";
return 0;
}
```

At first, that probably looks a bit weird, so here’s the general ternary operator structure:

• The code before the question mark is the condition (same as what goes in the parentheses of an if statement). Its value must be either true or false.
• The code after the question mark but before the colon is the code to run if the condition is true.
• The code after the colon is the code to run if the condition is false.

In this case, because “`temperature > 80`” is true, the sentence “It’s hot.” would be printed to the screen.

The ternary operator can also be used to set a variable equal to something depending on a condition. For example:

```#include <iostream>

int main() {
bool buttonPressed = true;
float motorSpeed = buttonPressed ? 0.6 : 0;
std::cout << motorSpeed << "\n";
return 0;
}
```

Here, `motorSpeed` is set to 0 because the condition `buttonPressed` is false. If `buttonPressed` were true, then `motorSpeed` would be set to 0.6.

As great as the ternary operator can be, always take care not to overuse it, as extensive use of the ternary operator can make code a bit harder to read.

### Challenge Problem

Write a program that performs a certain calculation depending on the value of an integer variable called `operation`. If `operation` is 0, perform addition; 1, perform subtraction; 2, perform multiplication; and 3, perform division. If `operation` is not equal to any of those values, print a message to the screen stating “No operation exists with the given value.” If the operation is division, make sure to avoid division by zero!

Perform the above operation on two other integer variables `num1` and `num2`, and print the result of the operation to the screen.

If you’d like, try using a switch statement when checking the possible values of the `operation` variable.

If you get stuck, check out my solution here.