← If / else with sensors lesson New set →

Robotics Worksheet: Robot If / Else

Robotics lesson 23: If / else with sensors · Set 171 · 40 questions
TalentJR

Robot tip (True → if, false → else): Put the reading into the condition: true → the if part, false → the else part. < means strictly less; an equal reading makes it false. else if: check the conditions from the top; the first true one wins. while: repeat until the condition is false.

NameDateTime takenScore ___ / 40
  1. 1.🤖 A robot is 91 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  2. 2.🤖 A robot is 54 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  3. 3.🤖 Obstacle avoiding: what should a robot do when something is close? (a) stop or turn away (b) speed up (c) push it hard (d) close its eyes
  4. 4.🤖 In “if (distance < 20)”, what runs when the distance is exactly 20? (a) neither part (b) both parts (c) the else part (d) the if part
  5. 5.🤖 if (distance < 20) { turn right } else { go forward }
    The distance sensor reads 21 cm. What does the robot do?
    (a) turn right (b) go forward
  6. 6.🤖 if (bumper is pressed) { back up and turn right } else { go forward }
    The bumper is not pressed. What does the robot do?
    (a) go forward (b) back up and turn right
  7. 7.🤖 if (bumper is pressed) { back up and turn left } else { go forward }
    The bumper is pressed. What does the robot do?
    (a) go forward (b) back up and turn left
  8. 8.🤖 A robot is 44 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  9. 9.🤖 A robot is 38 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How many times does it move forward?
  10. 10.🤖 if (distance < 30) { turn right } else { go forward }
    The distance sensor reads 25 cm. What does the robot do?
    (a) go forward (b) turn right
  11. 11.🤖 if (distance < 10) { turn right } else { go forward }
    The distance sensor reads 8 cm. What does the robot do?
    (a) go forward (b) turn right
  12. 12.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 24 cm and the light reading is 45. What does the robot do?
    (a) turn left (b) go forward (c) stop
  13. 13.🤖 if (distance < 30) { turn right } else { go forward }
    The distance sensor reads 24 cm. What does the robot do?
    (a) turn right (b) go forward
  14. 14.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 39 cm and the light reading is 25. What does the robot do?
    (a) go forward (b) stop (c) turn left
  15. 15.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 40 cm and the light reading is 75. What does the robot do?
    (a) turn left (b) stop (c) go forward
  16. 16.🤖 if (distance < 10) { turn right } else { go forward }
    The distance sensor reads 4 cm. What does the robot do?
    (a) go forward (b) turn right
  17. 17.🤖 A robot is 31 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  18. 18.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 5 cm and the light reading is 45. What does the robot do?
    (a) turn left (b) stop (c) go forward
  19. 19.🤖 if (distance < 30) { turn right } else { go forward }
    The distance sensor reads 22 cm. What does the robot do?
    (a) turn right (b) go forward
  20. 20.🤖 A robot is 80 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  21. 21.🤖 A robot is 34 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  22. 22.🤖 A robot is 42 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  23. 23.🤖 if (bumper is pressed) { back up and turn right } else { go forward }
    The bumper is pressed. What does the robot do?
    (a) back up and turn right (b) go forward
  24. 24.🤖 if (bumper is pressed) { back up and turn left } else { go forward }
    The bumper is not pressed. What does the robot do?
    (a) back up and turn left (b) go forward
  25. 25.🤖 if (distance < 20) { turn right } else { go forward }
    The distance sensor reads 16 cm. What does the robot do?
    (a) turn right (b) go forward
  26. 26.🤖 if (distance < 15) { turn right } else { go forward }
    The distance sensor reads 21 cm. What does the robot do?
    (a) turn right (b) go forward
  27. 27.🤖 A robot is 93 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How many times does it move forward?
  28. 28.🤖 if (distance < 10) { turn right } else { go forward }
    The distance sensor reads 6 cm. What does the robot do?
    (a) go forward (b) turn right
  29. 29.🤖 What does an “if” let a robot do? (a) skip all sensors (b) charge itself (c) choose using a sensor (d) go faster always
  30. 30.🤖 A robot is 55 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How many times does it move forward?
  31. 31.🤖 A robot is 49 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  32. 32.🤖 if (distance < 15) { turn right } else { go forward }
    The distance sensor reads 23 cm. What does the robot do?
    (a) turn right (b) go forward
  33. 33.🤖 A robot is 66 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How many times does it move forward?
  34. 34.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 25 cm and the light reading is 30. What does the robot do?
    (a) go forward (b) turn left (c) stop
  35. 35.🤖 if (distance < 15) { turn right } else { go forward }
    The distance sensor reads 12 cm. What does the robot do?
    (a) go forward (b) turn right
  36. 36.🤖 A robot is 85 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How far from the wall does it stop (in cm)?
  37. 37.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 34 cm and the light reading is 70. What does the robot do?
    (a) stop (b) turn left (c) go forward
  38. 38.🤖 if (distance < 20) { turn right } else { go forward }
    The distance sensor reads 13 cm. What does the robot do?
    (a) turn right (b) go forward
  39. 39.🤖 if (distance < 15) { stop } else if (light < 30) { turn left } else { go forward }
    The distance is 32 cm and the light reading is 20. What does the robot do?
    (a) turn left (b) go forward (c) stop
  40. 40.🤖 A robot is 79 cm from a wall. It runs:
    while (distance > 20) { forward 10 cm }
    stop
    How many times does it move forward?

Answer key

  1. 11
  2. 14
  3. stop or turn away
  4. the else part
  5. go forward
  6. go forward
  7. back up and turn left
  8. 14
  9. 2
  10. turn right
  11. turn right
  12. go forward
  13. turn right
  14. turn left
  15. go forward
  16. turn right
  17. 11
  18. stop
  19. turn right
  20. 20
  21. 14
  22. 12
  23. back up and turn right
  24. go forward
  25. turn right
  26. go forward
  27. 8
  28. turn right
  29. choose using a sensor
  30. 4
  31. 19
  32. go forward
  33. 5
  34. go forward
  35. turn right
  36. 15
  37. go forward
  38. turn right
  39. turn left
  40. 6

Free robotics lessons, practice and worksheets at talentjr.in/robotics-for-kids