The learner will review code and answer questions about the results of using a switch statement on a variable created using the map command.
All the flashcards in this set deal with the code below:
const int sensorMin = 0;
const int sensorMax = 100;
int range;
void setup()
{
Serial.begin(9600);
}
void main()
{
int sensorReading = analogRead(0);
range = map(sensorReading, sensorMin, sensorMax, 0, 3);
switch (range)
{
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println("bright");
break;
default:
Serial.println("invalid input");
}
}
This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.
All the flashcards in this set deal with the code below:
const int sensorMin = 0;
const int sensorMax = 100;
int range;
void setup()
{
Serial.begin(9600);
}
void main()
{
int sensorReading = analogRead(0);
range = map(sensorReading, sensorMin, sensorMax, 0, 3);
switch (range)
{
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println("bright");
break;
default:
Serial.println("invalid input");
}
}
This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.
Got it!
All the flashcards in this set deal with the code below:
const int sensorMin = 0;
const int sensorMax = 100;
int range;
void setup()
{
Serial.begin(9600);
}
void main()
{
int sensorReading = analogRead(0);
range = map(sensorReading, sensorMin, sensorMax, 0, 3);
switch (range)
{
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println("bright");
break;
default:
Serial.println("invalid input");
}
}
This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.
Got it!
How many unique cases does the switch statement in this program have? (Do not count the default case.)
How many unique cases does the switch statement in this program have? (Do not count the default case.)
4
There are four cases, case 0, case 1, case 2, and case 3.
How many unique cases does the switch statement in this program have? (Do not count the default case.)
4
There are four cases, case 0, case 1, case 2, and case 3.
Suppose our sensor reading maps to 2. What would be printed?
medium
If the reading is mapped to 2, we will be in case 2 which has lines Serial.println("medium"); and break; so the case 2 will print medium.
Suppose our sensor reading maps to 2. What would be printed?
medium
If the reading is mapped to 2, we will be in case 2 which has lines Serial.println("medium"); and break; so the case 2 will print medium.
True or False: The default case is executed if the range variable is not 0, 1, 2, or 3.
True
When a switch is done checking its cases, if it has not executed one of them, it will execute the code after default instead.
True or False: The default case is executed if the range variable is not 0, 1, 2, or 3.
True
When a switch is done checking its cases, if it has not executed one of them, it will execute the code after default instead.
True or False: In this example, more than one case can be true at the same time.
False
Once the sensor is read and the result is mapped to range, range will have exactly one value, either 0, 1, 2, or 3.
True or False: In this example, more than one case can be true at the same time.
False
Once the sensor is read and the result is mapped to range, range will have exactly one value, either 0, 1, 2, or 3.
True or False: Range could be of type float.
False
We should never use float variables for values we are going to test for equality or as arguments in functions that take integer arguments. The switch here is testing equality in each case, so it should not be used with a float variable.
True or False: Range could be of type float.
False
We should never use float variables for values we are going to test for equality or as arguments in functions that take integer arguments. The switch here is testing equality in each case, so it should not be used with a float variable.
If there were no break; command at the end of case 0, what would the output of that case be?
If there were no break; command at the end of case 0, what would the output of that case be?
dark & dim
So our program would look like:
case 0:
Serial.println("dark");
case 1:
Serial.println("dim");
break;
So, if we look at the code in case 0, the Serial.println("dark"); will print dark. Since the break; is missing, the switch will continue to execute the code it finds until it reaches a break; (or the end of the switch statement). Therefore, the code under case 1 will be executed as well; Serial.println("dim") will print dim, and then break; will end the switch. Thus, the program modified as above will print dark and then dim.
If there were no break; command at the end of case 0, what would the output of that case be?
dark & dim
So our program would look like:
case 0:
Serial.println("dark");
case 1:
Serial.println("dim");
break;
So, if we look at the code in case 0, the Serial.println("dark"); will print dark. Since the break; is missing, the switch will continue to execute the code it finds until it reaches a break; (or the end of the switch statement). Therefore, the code under case 1 will be executed as well; Serial.println("dim") will print dim, and then break; will end the switch. Thus, the program modified as above will print dark and then dim.
True or False: The case statement is somewhat like nested if - else statements.
True
Case statements function very much like if - else statements, first checking one condition, then checking another condition (else if) etc.
True or False: The case statement is somewhat like nested if - else statements.
True
Case statements function very much like if - else statements, first checking one condition, then checking another condition (else if) etc.
What would the output be if there were no default statement and range was equal to 4?
What would the output be if there were no default statement and range was equal to 4?
There would be no output
Without the default statement, a switch will not execute any of its code if it reaches the end of the case list without matching its input. So if the default statement was not here and range was 4, it would look through all the cases (0, 1, 2, and 3) and, not matching the value 4, would simply end.
What would the output be if there were no default statement and range was equal to 4?
There would be no output
Without the default statement, a switch will not execute any of its code if it reaches the end of the case list without matching its input. So if the default statement was not here and range was 4, it would look through all the cases (0, 1, 2, and 3) and, not matching the value 4, would simply end.
True or False: If range were of type Boolean, the case statements could be based on conditions such as TRUE, FALSE, HIGH, & LOW.
True or False: If range were of type Boolean, the case statements could be based on conditions such as TRUE, FALSE, HIGH, & LOW.
True
Boolean variables would match conditions like TRUE, FALSE, HIGH, LOW, 0, or 1.
True or False: If range were of type Boolean, the case statements could be based on conditions such as TRUE, FALSE, HIGH, & LOW.
True
Boolean variables would match conditions like TRUE, FALSE, HIGH, LOW, 0, or 1.
If the sensor input was 77, which case would this be mapped to?
2
When the map(sensorReading, sensorMin, sensorMax, 0, 3); function runs, it takes
(sensorReading - sensorMin) * (3 - 0) / (sensorMax - sensorMin) + 0
which for our values is
(77 - 0) * (3 - 0) / (100 - 0) + 0 = 2.31
Actually, it will give 2 since all the values being evaluated are int types, it will truncate the division to 2 (and then add 0).
Therefore the code range = map(sensorReading, sensorMin, sensorMax, 0, 3); in this case will make range = 2 and the switch will start executing the code under case 2.
If the sensor input was 77, which case would this be mapped to?
2
When the map(sensorReading, sensorMin, sensorMax, 0, 3); function runs, it takes
(sensorReading - sensorMin) * (3 - 0) / (sensorMax - sensorMin) + 0
which for our values is
(77 - 0) * (3 - 0) / (100 - 0) + 0 = 2.31
Actually, it will give 2 since all the values being evaluated are int types, it will truncate the division to 2 (and then add 0).
Therefore the code range = map(sensorReading, sensorMin, sensorMax, 0, 3); in this case will make range = 2 and the switch will start executing the code under case 2.
You have answered 5 of 10 questions correctly.
50%