Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Collections and Control Flow Control Flow With Loops While and Repeat While

Loops...

Hi, I don't know why but I just don't understand this loops. Can somebody help me understand this? Or give me a tip to understand it?

greetings from Lucas.

2 Answers

You use loops in real life all the time, you just might not realize you're doing it. Here are a couple of examples:

  • washing hands until they are clean (you repeat the washing motion until you are satisfied your hands are clean)
  • cooking pasta (you cook the pasta until it's soft and edible)
  • painting a wall (you repeat the painting action until the whole wall is covered)
  • going to work (you drive/walk/ride until you arrive)

That's all a loop really is. It's a group of instructions that are repeated a bunch of times. They can be repeated either a fixed number of times (brushing your hair 100 strokes before bed), or until a condition is met (ironing your shirt until all the wrinkles have disappeared).

Of course, in code, the actions and the condition are a bit more abstract, but the gist is the same.

This particular loop executes the code inside it a certain number of times (as it's counter-based). If you increment the counter, you are getting closer to the loop goal, when the goal is met, the code stops being executed.

The difference between while and repeat while is that while checks if the goal has been met before running the code inside it, and repeat while does it after it runs the code inside it.

Does that help? Feel free to ask more questions.

Thank you so much! How can i practice this the best?

Strictly code-wise, loops are fairly simple, there's not a lot to practice there. What you really want is to be able to recognize when a loop is required or recommended. One way to practice would be to start recognizing 'loop' actions in your normal day. Learn to identify which daily actions you do can be described as loops. When do you repeat actions? Being able to identify looped actions in real life will make it easier to understand (and use them) in the code.

As for code, create a new playground and just dive in. Experiment with conditions and actions. Include a line that prints out every time that the code inside the loop is executed so you can keep track of repetitions. The best way to learn and understand any programming aspect is to just dive in and experiment.