School is cancelled and the kids are home. Sledding is a great activity to get the kids out and moving on a snow day, but here's a simple coding activity to get your kids thinking. Make a peanut butter and jelly sandwich while learning about loops!
What are loops in coding?
Loop: A command used to repeat a part of code until a desired process is complete.
Loops are essential for many of the repetitive tasks commonly required in programming.
Programmers often need to repeat the same code hundreds of times in one program. We use loops to write the code only once and repeat it as many times as needed.
Why are loops important?
It is easy to make one peanut butter and jelly sandwich. But what if you needed to make 500 sandwiches for a school field trip? Instead of having to do the same steps over and over again, you would likely prefer to just do it once and have it automatically repeated until 500 sandwiches were made.
The main purpose of loops is to prevent repetitive code.
Typing out the same code over and over increases programming time and likeliness for bugs.
Use loops to keep your code DRY - this means you don't repeat yourself, which will make your code easier to manage and debug.
- Programmers don't spend hours writing the same line of code over and over.
- Less code to write means fewer programmer mistakes.
- Less code means less time a programmer spends debugging.
Loops in Programming Languages
Let's say you want to code a computer program that will print the words "Hello World!" 10 times. While this code is technically sound, it is not the most efficient way to tell the computer what to do.
With a loop, the statement needs to be written only once and will be executed 10 times.
Snow Day Coding Activity
The fuzzFamily is hungry! They are getting ready to go for a
hike through Smeeborg to explore their new planet, but they need to
get ready. Everyone has a job: simonFuzz is packing extra warm
clothes, gracieFuzz is packing the science kit to examine their
findings, blueFuzz is cleaning the ship, and rainbowFuzz is still
sleeping! Can you help the fuzzFamily get ready by making
sandwiches for the 4 hungry fuzzes to take on their adventure
In this coding activity, kids will follow an algorithm to repeat the same set of steps 4 times. Connect repeating the task with the use of loops and iterations in programming to save time and minimize errors.
Step 1 - Think about your algorithm
Think about the steps to create one peanut butter and jelly sandwich.
In a peanut butter and jelly sandwich algorithm, if you try to spread the jelly without first grabbing a knife, you will end up with no sandwich... and quite the mess!
Sequence is important in a computer algorithm because the correct order of steps is needed for the algorithm work.
Optional: Write down that you think will be the algorithm to make a PB&J sandwich.
Step 2 - Add a loop to your algorithm
We need to make four sandwiches, one for each fuzz. Since we are making four sandwiches, we will need to repeat the steps in our algorithm four times. Think about the steps we will need to repeat.
Optional: Write down that you think will be the algorithm to make 4 sandwiches.
Step 3 - Make your sandwiches - Run Your Code!
Use the worksheet here to practice the PB&J algorithm, or make lunch for the whole family and try using ingredients for your favorite sandwich at home!
Learn more about loops with Kodable