Get Help
If Statements
A Simple Yes or No Question (Can Be Complex)
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
- If/Else Statements
- Nested If Statements
- Conditions
- Combining Conditions
- Trying It with Sparki
If Statements (And Robots That Like Ice Cream)
An if statement in C++ is written like this:|
1 2 3 4 |
if (condition) { statement } |
|
1 2 3 4 |
if ( the ice cream is cold ) { buy that ice cream } |
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-
|
1 2 3 4 |
if ( the ice cream is cold && robot still has money ) { buy that ice cream } |
|
1 2 3 4 |
if ( the ice cream is rainbow flavored && robot still has money ) { buy that 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.|
1 2 3 4 5 6 7 8 |
if (condition) { statement1 } else { statement2 } |
|
1 2 3 4 5 6 7 8 |
if ( the ice cream is rainbow flavored && robot still has money ) { buy that ice cream } else { save the money for later } |
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-|
1 2 3 4 5 6 7 8 9 10 11 |
if (condition1) // first if statement { statement1 // code runs when first if statement is true if (condition2) // second if statement { statement2 // code that runs when both if statements are true } // end of second if statement } // end of first if statement |
|
1 2 3 4 |
if ( the ice cream is raspberry flavored ) { buy that ice cream } |
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!
|
1 2 3 4 5 6 7 8 9 |
if ( the ice cream is raspberry flavored ) { if ( the ice cream does not have a bite taken out of it ) { buy that ice cream } } |
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:|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <Sparki.h> // include the sparki library void setup() { } void loop() { int cm = sparki.ping(); // measures the distance with Sparki's eyes if (cm < 10) // if sparki measures a distance less than 10, show red { sparki.RGB(RGB_RED); } if (cm == 10) // if sparki measures equal to 10, show green { sparki.RGB(RGB_GREEN); } if (cm > 10) // if sparki measures a distance greater than 10, show show { sparki.RGB(RGB_BLUE); } } |


