Before we start writing code for Pro-Bot using Repeat Loops, let’s go through a few examples from your daily life. We shall write a few Repeat Loops in plain English to get some practice with identifying repeating patterns & end conditions.
For each of the questions below, write a Repeat Loop in plain English. You need to perform the following steps for each:
1. Identify the actions or patterns that repeat.
2. Identify the loop ending condition. This can be:
- the number of times that the pattern repeats or
- a specific condition that needs to be satisfied for the loop to stop.
Question 1
You decide to run 10 laps in the school cardio club today. You run around the field once for each lap. Can you write a Repeat Loop for this activity?
Action that repeats:
Loop ending condition:
Repeat _________ [
]
Question 2
You decide to run 10 laps in the school cardio club today. At the end of running each lap, you scan a card that tracks the number of laps. Can you write a Repeat Loop for this activity?
Actions that repeat:
Loop ending condition:
Repeat _________ [
]
Question 3
You reached school a bit after the cardio club started today. So, instead of running 10 laps, you decide to run till the first bell rings. Each time, you run around the field once and then scan a card that tracks the number of laps. Can you write a Repeat Loop for this activity?
Actions that repeat:
Loop ending condition:
Repeat ____________________________________________________ [
]
Question 4
You are going to the zoo with your family. You start from home at 9.00 am. It takes 40 minutes to drive from your home to the zoo. We will assume there are no traffic delays. You are really excited and can’t wait to get there. 10 minutes after the car starts, you ask “Are we there yet?”. Every ten minutes thereafter, you keep asking “Are we there yet?”, until you reach the destination at 9.40 am. (You ask the question at 9.40 am too, you haven’t found a parking space yet.) Can you write a Repeat Loop for this?
Action that repeats:
To find out how many times the action repeats, let’s write down the times at which you ask “Are we there yet?”
______________ am
______________ am
______________ am
______________ am
Loop ending condition:
Repeat _________ [
]
Question 5
You are going to the zoo again with your family. As usual, you are really excited and can’t wait to get there. But this time, there is a lot of traffic delay due to some construction work on the freeway. 10 minutes after the car starts, you ask “Are we there yet?”. Every ten minutes thereafter, you keep asking “Are we there yet?”, until you reach the destination. You know that you have reached the zoo when you see the huge “ZOO” sign by the road. At soon as you see the sign, you stop asking “Are we there yet?”. Can you write a Repeat Loop for this case?
Action that repeats:
Loop ending condition:
Repeat ______________________________________________________ [
]
Question 6
You are hungry and decide to eat some grapes for a snack. You eat only one grape at a time. You stop eating as soon as your stomach feels full. Can you write a Repeat Loop for this activity?
Action that repeats:
Loop ending condition:
Repeat ______________________________________________________ [
]
Question 7
You have to print the pattern ABABABABAB. Can you write a Repeat Loop for this activity?
Action that repeats:
Loop ending condition:
Repeat _________ [
]
Question 8
It's Halloween. You go from house to house, trick or treating. You get 2 candies from each house that you go to. Your mom has told you to return home once you collect 100 candies. Can you write a Repeat Loop for this activity?
Action that repeats:
Loop ending condition:
Repeat ________ [
]
Question 9
It's Halloween. You go from house to house, trick or treating. You have a big basket that you intend to fill up with candies. Your mom has told you to return home once your basket is full. Can you write a Repeat Loop for this activity?
Action that repeats:
Loop ending condition:
Repeat ______________________________________________________ [
]
No comments:
Post a Comment