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 } |
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 } |
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.
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 } |
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 } |
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 } } |
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); } } |