Why do we use loops?
We use loops to avoid writing repetitive code. Using loops makes our code cleaner, run smoother, and ultimately saves us a lot of time. When I looked at JavaScript on Codecademy, they had me write the same piece of code ten times to illustrate what precisely loops save you from doing. The point was well received. There are many kinds of loops, but we learned the for loop, the while loop, and the do… while loop. The for loop iterates through a block of code a known or specific number of times. The while loop iterates through a block of code while a specified condition is true – this means that the number of iterations does not necessarily have to be known. The do… while loop is similar to the while loop, also iterating through a block of code while a condition is true, but it is written in reverse of the while loop (the condition is last, not first). I am most familiar with the for loop, and therefore find it to be the most intuitive. However, mastering the three parts, as shown in the above image, can be somewhat tricky!
Hi Julia! That’s super interesting. I actually went onto that website you mentioned and tried it out myself and it definitely helps understand the point of loops and to crucially look at why a loop is used in code. I definitely agree that I am most familiar with the for loops after this class as well as in-class activities. I think the repetitiveness of the loops is what truly made me be able to understand how and why we use them. It’s also good that loops help make the code manageable and stay organized. It is definitely easy to get lost in the sauce of coding.