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 Looping Over Ranges

I do not understand what is the goal of the course(loop)?

I need more explanations of this course: 1- where can I use this course (which type of apps) 2- what is the difference between while loop and repeat loop 3- are this lessons (loop) important to create apps thanx for your helps

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey abdulaziz fak,

Loops are extremely important. You will use them everywhere in your apps. It helps you write less repetitive code by allowing you to execute the same piece of code multiple times. The difference between a while loop and a repeat while loop is this:

/* While Loop
This loop evaluates x <= 100
If true, it will execute the code x + 1, then it will evaluate x <= 100 again
If true, it will execute x + 1 again and it will continue to do this until
x <= 100 evaluates to false (which is when x is greater than 100) */

var x = 0
while x <= 100 {
    x + 1

/* Repeat While Loop
This loop executes x + 1 first
THEN it evaluates x <= 100 afterwards
If true, it will execute x + 1 again, and then evaluate for x <= 100 again
It will continue this until x <= 100 evaluates to false */ 

var x = 0
repeat {
    x + 1
} while x <= 100

As you can see, the two loops are similar. The main difference is that the repeat loop will execute the statement before it checks the while condition. This means that a repeat while loop will always execute at least once.

Good Luck!