# If Statements

SparkiDuino
Easy

### Lessons You Should Know

If statements are a way to ask a computer or robot a question. If statements are the basis for most computer logic. In this lesson we’ll be covering:

#### If Statements (And Robots That Like Ice Cream)

An if statement in C++ is written like this: An if statement works by testing a condition. If the condition is true, it runs the statement inside the brackets. If the condition is false, the statement inside the brackets does not run. So, let’s take a look at how this might help our robot friend choose which ice cream it should buy. We’ll use something called pseudocode, which is imaginary code that won’t run on a robot but which will help us humans think about how the code works- If the code above is the only code the robot is running it won’t really help the robot make a decision, will it? Because the answer to the condition (is the ice cream cold?) is going to be “yes” or true for every type of ice cream! So the robot will automatically buy the first ice cream it comes across and then continue to try and buy more ice cream even though it may have run out of money after buying the first ice cream! That would make the ice cream vendor pretty mad. Luckily you can combine conditionals and if statements to ask more than one question at a time. We’ll get more into this later, but let’s use it really quick to help our robotic ice cream buying friend out. To combine multiple conditions (remember, that’s just a fancy word for question) you use two ampersand symbols &&. Here’s some pseudocode asking the robot if the ice cream is cold and it still has money-

But there is a problem with this code too! The robot isn’t really making a decision, is it? It just buys whichever ice cream it sees first. When you’re writing your conditionals you have to think hard about the best way to get the robot to do what you want. Let’s write some code that will make the robot choose a specific type of ice cream each time it goes to buy ice cream-

Nice. Now our robot is happy. This code will make it find its favorite ice cream each time, no matter where the rainbow flavored ice cream happens to be. Let’s move on and talk about what the robot should do if there is no rainbow flavored ice cream….

#### If Else Statements

You can do more with if than just run code if the condition is true. You can also use an else to run code if it is false. This code checks to see if condition is true. If it is true, it runs the statement1 inside those curly brackets. If it is false, it runs the statement2 code inside those curly brackets. Let’s see what this type of code looks like for our ice cream craving robot. Of course there are other things we could put in the robot’s else statement than saving the money. The robot might have a second choice for ice cream flavor in the else statement, or maybe the robot would go and get a chicken salad instead?

#### Try it with Sparki

Here is an example that uses an if-else statement. This code uses Sparki’s ultrasonic range finder to measure how far something in front of it is. This code measures the distance, the changes the RGB color:

or

## Next Step:

Next we’ll take a look at while statements. They’re sort of like if statements, except they keep happening over and over again until the answer to the condition is false.