In the code snippet switch (2) { case 1: digitalWrite(11,HIGH); case 2: analogRead(A3) }, which action occurs?

Study for the Internet of Things (IOT102) Test. Access comprehensive flashcards and multiple choice questions, each with hints and explanations. Get prepared for your exam!

Multiple Choice

In the code snippet switch (2) { case 1: digitalWrite(11,HIGH); case 2: analogRead(A3) }, which action occurs?

Explanation:
Switch statements in C/C++ use fall-through behavior when there are no break statements. With the expression evaluating to 2, execution starts at the block labeled for the value 2, which contains the call to read the analog value from pin A3. Since there is no break after that line, the code would continue to execute any subsequent statements inside the switch if there were any; however, in this snippet there aren’t additional actions to perform. Therefore, the action that occurs is reading the analog value from A3. The line that would set a digital pin high would only run if execution had started at the earlier case (or fell through from it), which doesn’t happen here because the expression is 2.

Switch statements in C/C++ use fall-through behavior when there are no break statements. With the expression evaluating to 2, execution starts at the block labeled for the value 2, which contains the call to read the analog value from pin A3. Since there is no break after that line, the code would continue to execute any subsequent statements inside the switch if there were any; however, in this snippet there aren’t additional actions to perform. Therefore, the action that occurs is reading the analog value from A3. The line that would set a digital pin high would only run if execution had started at the earlier case (or fell through from it), which doesn’t happen here because the expression is 2.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy