Self Assessment: IF Statement Quiz 2

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%;
else
    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.

Click Next for the answers.

 

Navigation: