The learner will review code and answer questions about the results of using a switch statement on a variable created using the map command.

Switch Statement on 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.

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

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.

question image

Correct Answer

Got it!

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

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.

question image

Your Answer

Correct Answer

Got it!

 Next Question
Question 1 of 10

Switch Statement on Map Command

How many unique cases does the switch statement in this program have?  (Do not count the default case.)

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

How many unique cases does the switch statement in this program have?  (Do not count the default case.)

question image

Correct Answer

4

Explanation:

There are four cases, case 0, case 1, case 2, and case 3.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

How many unique cases does the switch statement in this program have?  (Do not count the default case.)

question image

Your Answer

Correct Answer

4

Explanation:

There are four cases, case 0, case 1, case 2, and case 3.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Suppose our sensor reading maps to 2.  What would be printed?

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

Suppose our sensor reading maps to 2.  What would be printed?

question image

Correct Answer

medium

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

Suppose our sensor reading maps to 2.  What would be printed?

question image

Your Answer

Correct Answer

medium

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

True or False: The default case is executed if the range variable is not 0, 1, 2, or 3.  

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

True or False: The default case is executed if the range variable is not 0, 1, 2, or 3.  

question image

Correct Answer

True

Explanation:

When a switch is done checking its cases, if it has not executed one of them, it will execute the code after default instead.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

True or False: The default case is executed if the range variable is not 0, 1, 2, or 3.  

question image

Your Answer

Correct Answer

True

Explanation:

When a switch is done checking its cases, if it has not executed one of them, it will execute the code after default instead.

 Next Question
Question 1 of 10

Switch Statement on Map Command

True or False: In this example, more than one case can be true at the same time.

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

True or False: In this example, more than one case can be true at the same time.

question image

Correct Answer

False

Explanation:

Once the sensor is read and the result is mapped to range, range will have exactly one value, either 0, 1, 2, or 3.  

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

True or False: In this example, more than one case can be true at the same time.

question image

Your Answer

Correct Answer

False

Explanation:

Once the sensor is read and the result is mapped to range, range will have exactly one value, either 0, 1, 2, or 3.  

 Next Question
Question 1 of 10

Switch Statement on Map Command

True or False: Range could be of type float.

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

True or False: Range could be of type float.

question image

Correct Answer

False

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

True or False: Range could be of type float.

question image

Your Answer

Correct Answer

False

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

If there were no break; command at the end of case 0, what would the output of that case be?

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

If there were no break; command at the end of case 0, what would the output of that case be?

question image

Correct Answer

dark & dim

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

If there were no break; command at the end of case 0, what would the output of that case be?

question image

Your Answer

Correct Answer

dark & dim

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

True or False: The case statement is somewhat like nested if - else statements.

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

True or False: The case statement is somewhat like nested if - else statements.

question image

Correct Answer

True

Explanation:

Case statements function very much like if - else statements, first checking one condition, then checking another condition (else if) etc.  

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

True or False: The case statement is somewhat like nested if - else statements.

question image

Your Answer

Correct Answer

True

Explanation:

Case statements function very much like if - else statements, first checking one condition, then checking another condition (else if) etc.  

 Next Question
Question 1 of 10

Switch Statement on Map Command

What would the output be if there were no default statement and range was equal to 4?

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

What would the output be if there were no default statement and range was equal to 4?

question image

Correct Answer

There would be no output

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

What would the output be if there were no default statement and range was equal to 4?

question image

Your Answer

Correct Answer

There would be no output

Explanation:

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.

 Next Question
Question 1 of 10

Switch Statement on Map Command

True or False: If range were of type Boolean, the case statements could be based on conditions such as TRUE, FALSE, HIGH, & LOW.

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

True or False: If range were of type Boolean, the case statements could be based on conditions such as TRUE, FALSE, HIGH, & LOW.

question image

Correct Answer

True

Explanation:

Boolean variables would match conditions like TRUE, FALSE, HIGH, LOW, 0, or 1. 

 Next Question
Question 1 of 10

Switch Statement on Map Command

Incorrect

True or False: If range were of type Boolean, the case statements could be based on conditions such as TRUE, FALSE, HIGH, & LOW.

question image

Your Answer

Correct Answer

True

Explanation:

Boolean variables would match conditions like TRUE, FALSE, HIGH, LOW, 0, or 1. 

 Next Question
Question 1 of 10

Switch Statement on Map Command

If the sensor input was 77, which case would this be mapped to?

Question Image
Question 1 of 10

Switch Statement on Map Command

Correct!

If the sensor input was 77, which case would this be mapped to?

question image

Correct Answer

2

Explanation:

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.


 Finish
Question 1 of 10

Switch Statement on Map Command

Incorrect

If the sensor input was 77, which case would this be mapped to?

question image

Your Answer

Correct Answer

2

Explanation:

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.


 Finish
Question 1 of 10
Switch Statement on Map Command

Score

You have answered 5 of 10 questions correctly.

50%

Start Over

See other flashcards and apps related to:

Question 1 of 10
Author
Published
6/2/2015
Last Updated
6/2/2015
Tags

No Comments Yet

Creative Commons License
Switch Statement on Map Command by Fox Valley Technical College is licensed under a Creative Commons Attribution 3.0 Unported License.