6 Loops
Loops help with repetitive tasks. They execute a set of code repeatedly, either for a given number of iterations or as long as a defined condition is met. Loops can save you from writing tedious and repetitive code, so take note! You will be using them a lot in your development.
In this chapter, you will use two sorts of loops:
-
forloops -
whileloops
for loops are ideal for iterating over the specific elements of an instance or collection of instances if the number of iterations to perform is either known or easy to derive.
while loops, on the other hand, are well suited for tasks that execute repeatedly as long as a certain condition is met.
Each type of loop has variations.
Let’s start with a for-in loop, which ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access