The learner will review code and answer questions about a switch statement.

Switch Statement Utilizing Character Input

All the flashcards in this set deal with the code below:

int currentPin;

void setup()
{
   Serial.begin(9600);

   for (currentPin = 0; currentPin < 8; currentPin++)
      pinMode(currentPin, OUTPUT);

}

void main()
{
   char inByte;

   if (Serial.available() > 0)
   {
      inByte = Serial.read();

      switch (inByte)
      {

         case 'a':
            digitalWrite(1, HIGH);
            break;
         case 'b':
            digitalWrite(2, HIGH);
            break;
         case 'c':
            digitalWrite(4, HIGH);
            break;
         case 'd':
            digitalWrite(5, HIGH);
            break;
         case 'e':
            digitalWrite(7, HIGH);
            break;
         default:
            for (currentPin = 0; currentPin < 8; currentPin++)
               digitalWrite(currentPin, LOW);

      }
   }
}

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 Utilizing Character Input

Correct!

All the flashcards in this set deal with the code below:

int currentPin;

void setup()
{
   Serial.begin(9600);

   for (currentPin = 0; currentPin < 8; currentPin++)
      pinMode(currentPin, OUTPUT);

}

void main()
{
   char inByte;

   if (Serial.available() > 0)
   {
      inByte = Serial.read();

      switch (inByte)
      {

         case 'a':
            digitalWrite(1, HIGH);
            break;
         case 'b':
            digitalWrite(2, HIGH);
            break;
         case 'c':
            digitalWrite(4, HIGH);
            break;
         case 'd':
            digitalWrite(5, HIGH);
            break;
         case 'e':
            digitalWrite(7, HIGH);
            break;
         default:
            for (currentPin = 0; currentPin < 8; currentPin++)
               digitalWrite(currentPin, LOW);

      }
   }
}

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 Utilizing Character Input

Incorrect

All the flashcards in this set deal with the code below:

int currentPin;

void setup()
{
   Serial.begin(9600);

   for (currentPin = 0; currentPin < 8; currentPin++)
      pinMode(currentPin, OUTPUT);

}

void main()
{
   char inByte;

   if (Serial.available() > 0)
   {
      inByte = Serial.read();

      switch (inByte)
      {

         case 'a':
            digitalWrite(1, HIGH);
            break;
         case 'b':
            digitalWrite(2, HIGH);
            break;
         case 'c':
            digitalWrite(4, HIGH);
            break;
         case 'd':
            digitalWrite(5, HIGH);
            break;
         case 'e':
            digitalWrite(7, HIGH);
            break;
         default:
            for (currentPin = 0; currentPin < 8; currentPin++)
               digitalWrite(currentPin, LOW);

      }
   }
}

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 Utilizing Character Input

True or False: All lights will be turned off if the character 'A' is received.

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

True or False: All lights will be turned off if the character 'A' is received.

question image

Correct Answer

True

Explanation:

Since Serial.read returns the first byte of information and 'A' is not represented by the same byte as 'a', the switch statement will go to its default case and set all the pins to LOW.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

True or False: All lights will be turned off if the character 'A' is received.

question image

Your Answer

Correct Answer

True

Explanation:

Since Serial.read returns the first byte of information and 'A' is not represented by the same byte as 'a', the switch statement will go to its default case and set all the pins to LOW.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Which output is set to high if the character 'b' is received?

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

Which output is set to high if the character 'b' is received?

question image

Correct Answer

2

Explanation:

This is case 'b', so the code under it will be executed, first digitalWrite(2, HIGH); setting output 2 to high, then break; ending the switch statement.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

Which output is set to high if the character 'b' is received?

question image

Your Answer

Correct Answer

2

Explanation:

This is case 'b', so the code under it will be executed, first digitalWrite(2, HIGH); setting output 2 to high, then break; ending the switch statement.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Will the switch statement be executed if there is no serial input?

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

Will the switch statement be executed if there is no serial input?

question image

Correct Answer

No

Explanation:

The switch statement will only execute if there is actual input, which is what Serial.available() > 0 is checking for.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

Will the switch statement be executed if there is no serial input?

question image

Your Answer

Correct Answer

No

Explanation:

The switch statement will only execute if there is actual input, which is what Serial.available() > 0 is checking for.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

If the input received is 'E', will digital output 7 be turned on?

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

If the input received is 'E', will digital output 7 be turned on?

question image

Correct Answer

No

Explanation:

Since Serial.read returns the first byte of information and 'E' is not represented by the same byte as 'e', the switch statement will go to its default case and set all the pins to LOW.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

If the input received is 'E', will digital output 7 be turned on?

question image

Your Answer

Correct Answer

No

Explanation:

Since Serial.read returns the first byte of information and 'E' is not represented by the same byte as 'e', the switch statement will go to its default case and set all the pins to LOW.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

True or False: The variable inByte could be of type integer and the program would function the same.

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

True or False: The variable inByte could be of type integer and the program would function the same.

question image

Correct Answer

True

Explanation:

Since Serial.read returns the first byte of information, it will read the inputs and compare them to the cases (as written) the same if inByte is a char variable type or an int variable type.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

True or False: The variable inByte could be of type integer and the program would function the same.

question image

Your Answer

Correct Answer

True

Explanation:

Since Serial.read returns the first byte of information, it will read the inputs and compare them to the cases (as written) the same if inByte is a char variable type or an int variable type.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

What is the scope of the currentPin variable?

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

What is the scope of the currentPin variable?

question image

Correct Answer

Global

Explanation:

The currentPin variable is declared outside of any of the functions, so it has global scope.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

What is the scope of the currentPin variable?

question image

Your Answer

Correct Answer

Global

Explanation:

The currentPin variable is declared outside of any of the functions, so it has global scope.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

The decision that determines which output is turned on is based on what?

Question Image
Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

The decision that determines which output is turned on is based on what?

question image

Correct Answer

inByte

Explanation:

The switch command has inByte as its argument, so the value of inByte is what the cases are checked against.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

The decision that determines which output is turned on is based on what?

question image

Your Answer

Correct Answer

inByte

Explanation:

The switch command has inByte as its argument, so the value of inByte is what the cases are checked against.

 Next Question
Question 1 of 10

Switch Statement Utilizing Character Input

Question 1 of 10

Switch Statement Utilizing Character Input

Correct!

The switch statement is used to handle what?

question image

Correct Answer

data that is char, Boolean, or integer type

Explanation:

The argument of the switch function needs to be easily comparable using an equality comparison, which limits our options (basically) to those in the correct answer: char, Boolean, and int.

 Finish
Question 1 of 10

Switch Statement Utilizing Character Input

Incorrect

The switch statement is used to handle what?

question image

Your Answer

Correct Answer

data that is char, Boolean, or integer type

Explanation:

The argument of the switch function needs to be easily comparable using an equality comparison, which limits our options (basically) to those in the correct answer: char, Boolean, and int.

 Finish
Question 1 of 10
Switch Statement Utilizing Character Input

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 Utilizing Character Input by Fox Valley Technical College is licensed under a Creative Commons Attribution 3.0 Unported License.