Congratulations! We are almost done with this lesson, but before we finish, let’s review loops.
Loops instruct the computer to repeat an activity without duplicating any code. Coders use loops to repeat actions. We discussed three types of loops:
- While loops
- For loops
- For-each loops
When we don’t know how many times the loop needs to be repeated, we use a while loop. A while loop repeats as long as a certain condition is met.
For-loops are used when we know how many times we want the code to repeat. We supply the code with a value, and the computer completes the task that amount of times.
For Each Loops
For each loops perform actions on each item in a list.
To repeat actions, use loops in your next project!
Try using different loop blocks to guide Codey to the flag. This can be solved using any of the loop types. Can you solve it using each loop type?
There are two main ways to solve this:
- Codey can move up, to the right, and then down.
- Code can move down, to the right, then up.
For 4 loops Move up For 2 loops Move right While Codey has not reached goal Move down