Instructional Material: IF Statement Quiz 2

Printer-friendly version

Example of if statement misuse.

Lets say I have a robot that has a servo that I want to control using two buttons on a controller, and I want the following rules to be followed:

-If button A is pressed, the servo extends all the way

-if button B is pressed, the servo extends halfway

-if no button is pressed the servo retracts

Here is a pseudocode implementation of these rules:

if A.isPressed()
    servo extends 100%;

if B.isPressed()
    servo extends 50%;
    servo extends 0%;

Consider the case when button A is pressed, but B is not. What will the behavior of this code result in? Fix the code using appropriate changes to the conditionals.

Here are the answers.


Material Type: 
Lab Activity
Education Level: 
HW Platform: 
SW Platform: 
Interactivity Style: 
Copyright (c) 2017 by the author. All rights reserved.