D. Watch the video: The Wait-until-event Pattern
1. Which scenario best fits an indefinite loop pattern?
a. The robot needs to go forward until it bumps into a wall.~
b. The robot needs to move in a square pattern.
c. The robot needs to use a sensor.
d. The robot falls off a table.
2. Which scenario best fits a definite loop pattern?
a. The robot needs to go forward until it bumps into a wall.
b. The robot needs to move in a square pattern.~
c. The robot needs to use a sensor.
d. The robot falls off a table.
3. Write a definite loop (using a FOR statement) that prints the numbers 0, 1, 2, ... 99.
M. Line 1 -> for i in range(100):
M. Line 2 -> print(i)
M. -> for i in range(99)
M. -> for i in range(101)
M. -> print(n)
4. Write an indefinite loop (using a WHILE statement) that prints the numbers 0, 1, 2, ... 99. (Note: Write it in such a way that it would work even if no numbers were to be printed. If this note makes no sense to you, just do the problem and you will see what I mean.)
M. Line 1 -> i = 0
M. Line 2 -> while True:
M. Line 3 -> if i > 99:
M. Line 4 -> break
M. Line 5 -> print(i)
M. Line 6 -> i = i + 1
M. -> i = i - 1
M. -> i = 1
M. -> return
M. -> print(n)
M. -> while true:
5. Write an indefinite loop (using a WHILE statement) that prints integers starting at 100,000 and stopping when it encounters an integer whose cosine is less than -0.999. Do not print the integer whose cosine is less than -0.999. Assume that an import math statement has already been written elsewhere in the code.
M. Line 1 -> i = 100000
M. Line 2 -> while True:
M. Line 3 -> if math.cos(i) < -0.999:
M. Line 4 -> break
M. Line 5 -> print(i)
M. Line 6 -> i = i + 1
M. -> i = 99999
M. -> i = i - 1
M. -> i = 0
M. -> return
M. -> print(n)
M. -> while true:
M. -> if math.cos(i) > -0.999:
10. In the previous problem you were not to print the integer whose cosine is less than -0.999. How would you modify your answer above if you were supposed to print the integer whose cosine is less than -0.999, but still stop the loop after doing so?
M. Line 1 -> i = 100000
M. Line 2 -> while True:
M. Line 3 -> print(i)
M. Line 4 -> if math.cos(i) < -0.999:
M. Line 5 -> break
M. Line 6 -> i = i + 1
M. -> i = 99999
M. -> i = i - 1
M. -> i = 0
M. -> return
M. -> print(n)
M. -> while true:
M. -> if math.cos(i) > -0.999: