Before we get to writing our own loops, let’s explore what programming would be like if we couldn’t use loops.
Let’s say we want to make a program that prints
"Hip Hip Hooray!" 5 times:
print("Hip Hip Hooray!") print("Hip Hip Hooray!") print("Hip Hip Hooray!") print("Hip Hip Hooray!") print("Hip Hip Hooray!")
That’s still manageable, we’re writing 5
print() statements (or copying and pasting a few times). But, imagine if we come back to this program and we want to change it to print
"Hip Hip Hooray!" 10, or 24601, or … 100000000 times? It would take an extremely long time and by the end, we could still end up with inconsistencies and mistakes.
Don’t dwell too long on this tedious scenario — we’ll learn how loops can help us out in the next exercise. For now, let’s gain an appreciation for loops. 😈
print() statements, print out:
"This can be so much easier with loops!".