Beginner Coding with Blocks
Chapter
>
Level
Conditions and Decisions
Nested Conditions in Loops
Objective
Clean out some weeds in the passage between buildings using nested conditions in loops.
The passage between buildings has weeds growing, gather all the weeds and dump them by using loops with nested conditions. There are ways you can automate this process by using conditions inside loops.
Create a variable named bag and set it to 0. Use a while loop with a condition to automate the process, there are ten (10) weeds in total. Use the while loop block with a condition that checks if bag is less than 10. Inside the loop, move forward and use the change variable by block to add 1 to bag.
By using a condition in the while loop, the loop will continue as long as the condition is true. Each loop cycle adds a weed to the bag and the loop automatically stops when bag reaches 10.
After navigating the passage, use a for loop to dump the weeds into containers by using the place block at the X marks. The amount of steps between each container is uneven though as there is a gap in between. By using nested if blocks inside the loop, you can skip certain code when a condition is met.
Use a for loop block with variable x from 0 to 2. Inside the loop, move forward, then use an if block to check if x is NOT equal to 1. Only when this condition is true (i.e., when x is 0 or 2), execute the code to turn right, place weeds, and turn left. This way you skip the middle iteration (when x is 1) to avoid the gap between containers.
Since there are two (2) crates, use the place block to dump the weeds stored in the bag variable by dividing the total. Use the divide math block to calculate bag / 2.
Once you've collected and placed the weeds in the proper location, reach the exit marked by a star to complete the level.