If Statements

A Simple Yes or No Question (Can Be Complex)

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- robotIfCold 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-

robotIfColdMoney2

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-

robotIfColdRainbow3

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. robotIfElseIceCream2 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?

Nested If Statements

You can even put if statements inside of if statements! This happens a lot and it’s called “nesting” if statements. (Really you can nest any control statement.) With nested if statements the robot can ask one question (the first if statement) and if the answer is “yes” then it can ask other questions inside of the first if statement. If the answer to the first if statement was “no” then the robot will skip the second if statement because it is inside of the first one. The way we write nested if statements looks like this- For this ice cream based example code let’s pretend that our robot is going to a new ice cream vendor and it now likes raspberry ice cream. The robot’s friends have also told the robot that this new ice cream vendor has a tendency to take bites out of the ice cream he sells! So, now the robot’s code needs to make sure that it doesn’t wind up with any ice cream that already has a bite taken out of it. Here’s the first if statement so the robot winds up with its new favorite flavor, raspberry- robotNestedIf Next we need to insert another nested if statement to help the robot avoid buying ice cream with a bite already taken out of it. An ice cream with a bite already taken out of it is just gross! robotNestedIf2 Okay! Our nested if statements worked and the robot wound up with some raspberry ice cream. (That other pink ice cream is strawberry flavored.) Nested control statements are everywhere and they’re very useful. There’s also no limit to how many you put inside of each other! Next up, we’ll learn more about the conditions (the questions) that go inside of the if statement parentheses.

Let’s Take a Break to Talk About Conditions

Click here to go learn about conditions. If you feel like you already understand conditions (even those complicated ones that ask multiple questions!) keep going.  

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:

Recap of What We’ve Covered so Far

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.

Next Lesson – While Statements