Coding 5: Nested Repeats


#1

Coding Threads

Coding 0: Welcome To Coding Camp. Installing Computercraftedu and using the Turtle Remote
Coding 1: Enter The Turtle Garden. Learning how to control your Turtle.
Coding 2: The Enchanted Forest. Practicing Turtle movement.
Coding 3: Building And Repeats. Learning to build with your Turtle.
Coding 4: The Enchanted Meadow. Practicing Building and Repeats.
Coding 5: Nested Repeats. Learning to stack repeats.
Coding 6: Turtle Town. Practicing repeats.
Coding 7: The Big Dig. Learning to dig and practicing loops.
Coding 8: Million Mile Mines. Practicing digging and loops.
Coding 9: The If Statement. Learning about conditional statements.
Coding 10: The Working Turtle. Writing complex programs with conditions and loops.

Nesting Loops

Imagine you’re having a party and you want your Turtle to help make cookies. Coding a Turtle to make one batch of cookies is easy enough, your directions might look something like this:

PLACE flour, sugar, eggs, and chocolate chips.
REPEAT 40 DO
mix
END
PLACE the mix in the oven.

You realize though that you will need more than one batch of cookies. Your Turtle will need to make 10 batches of cookies! You can ask the Turtle to repeat all of the directions together by putting them inside of a REPEAT.

REPEAT 10 DO
PLACE flour, sugar, eggs, and chocolate chips.
REPEAT 40 DO
mix
END
PLACE the mix in the oven.
END

Putting a REPEAT inside of another REPEAT is called nesting. Nesting REPEATS is a great way to make a building. Check out the Turtle working below:

<img src=http://drive.google.com/uc?export=view&id=0Byh5jOX2Lh-FRk01UVFnUUdOd2M>

Without nesting REPEATS the code is complicated and messy, like this:

<img src=http://drive.google.com/uc?export=view&id=0Byh5jOX2Lh-FN0J6QUZTVXlxZjA>

Because we are REPEAT the same directions 4 times we can use a nested REPEAT for cleaner, better looking code:

<img src=http://drive.google.com/uc?export=view&id=0Byh5jOX2Lh-FUGFXRnNFS0F1akU>

In both examples the Turtle will PLACE and MOVE FORWARD ten times before TURNING RIGHT and REPEATING.

Coding Challenges

A Nested Repeat Puzzle: Stack your repeat loops in the Nested Repeat Puzzle. Puzzle #1 is the easiest and puzzle #5 is the hardest.

Main Builder Course: Your Turtle’s first building. Use nested repeat loops to create buildings of 3 different sizes.


Coding 0: Welcome To Coding Camp
Coding 3: Building And Repeats
Coding 1: Enter The Turtle Garden
Coding 2: The Enchanted Forest
Coding 4: The Enchanted Meadow

Back to Connected Camps Homepage