Get Help
Recap of Switch Statements
What We’ve Covered So Far
Here’s what a switch statement looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch ( parameter ) { case value1: statement1; break; case value2: statement2; statement3; break; default: statement4; break; } |
The break; command at the end of each case statement causes the switch statement to stop running code inside the switch statement.
The default statement at the end of the case statements runs if the parameter doesn’t match any of the other case statements.
Questions and Activities to Try
1. Create a switch statement that will print different words or sentences on the LCD screen depending on the value of a variable. Change the variable and reload the code a couple times to see if it works. Click here for help with printing words and sentences out on the LCD screen.2. Now try using Sparki’s Ultrasonic Distance Sensor and storing the value you get from a reading in a variable. Then use that variable to control your switch statement. You may need to change the case statements that you have so that they use the full range reached by the Ultrasonic sensor.