How to work with Loops

Hi guys!!!

I was going thru Loops today and it has me on a loop. Iā€™m having a hard time understanding how they work and/or the purpose on them. If anyone has an idea please share!!!

Thank you :upside_down_face:

Loops are used where you need to interate over data or do something repetitively.

For example, if you had to deal with a list of data that contained names and addresses and you wanted to display them in a table format on screen (a bit like you would see in a spreadsheet) then in broad terms you would loop through the data and list each name and address in turn until you got to the end.

A practical example is an array of names such as this:

let names = ["John","Paul","Ringo","George"]

If you wanted to print each name you would do this

for name in names {
    print(name)
}

you would read that as "For each name in the list of names print the name.

Does that make sense?

1 Like