In Part 3, I showed you how you already know about functions. Here’s even more you already know about coding.
You Already Know About Loops
“I repeat, I don’t know this stuff…”
If you’ve ever performed a task more than once, you’ve enacted a loop. For example, take skipping. You swing the rope and when the rope is about to hit your feet, you jump. Then you repeat the same motion again and again… until you’re tired. The sequence of actions could be represented like this:
- Swing the rope
- When rope is almost touching feet, jump
- Swing the rope
- When rope is almost touching feet, jump
- Swing the rope
- When the rope is almost touching feet, jump
- Start to feel tired
- Stop skipping
That list of actions was cumbersome to write, and it would be in code too. So instead of repeatedly writing the same steps, we use a loop instead. The sequence of actions becomes:
- Swing the rope
- When rope is almost touching feet, jump
- If not tired, go back to 1. Otherwise, stop skipping
This is much shorter! Notice how we introduced a conditional (If not tired…). We learned that we already knew about those in Part 1. The conditional allows us to determine if we want to continue looping or stop.
Let’s see how we would represent this in code:
tired = False count = 0 while tired is False: swing_the_rope = True jump = True count = count + 1 print("skip") if count >= 10: tired = True print("tired, stopping")
First we set some variables. The ‘tired’ variable is set to False initially. When we start skipping, we aren’t tired yet. We also set the count to 0. This will keep track of how many skips we’ve done. Then we enter a ‘while’ loop. The ‘while’ loop will execute while the conditional we set for the while loop remains true. So while the variable ‘tired’ is False, we will keep executing the loop (i.e. keep skipping). Entering the loop, we perform the actions of ‘swing_the_rope’ and ‘jump’. Then we add one to the count, as we’ve performed a skip. We then check the count to see if we’ve done 10 skips or more. If we have, we set the variable ‘tired’ to true (after 10 skips, we’re tired). If we haven’t done 10 skips yet, the code will go back to executing the while block from the start. So we will perform the ‘swing_the_rope’ and ‘jump’ actions again and again until count reaches 10. At that point, we are tired (and set the variable ‘tired’ to True). When the while loop checks the conditional before continuing, it will find that ‘tired’ is now True, so the conditional equates to False (tired is not False), so the loop ends.
If we were to run this code, the output would be:
skip skip skip skip skip skip skip skip skip skip tired, stopping
Conclusion
There are different ways to write loops and control when they end, but this gives you an example of how loops can be used to save on writing the same code multiple times. They can be a powerful addition to your coding toolkit.
Are you convinced yet, that you already know how to code? Read on in You Already Know How to Code: Part 5, or if you want to learn more, I recommend trying the free courses at CodeCademy or SoloLearn. See you next time!
Recent Comments