# 7. Loops in Swift

If some one ask you to print numbers from 0 to 5,  you can print,

```println(0)
println(1)
println(2)
println(3)
println(4)
println(5)
```

its easy but what if some one ask you to do it 100 times or 1000 times or more. Then printing lines one by one is not a good way to solve this problem. You need some solution which can perform one task repeatedly as long as its required.

Swift has very famous solution for this problem called Loops. Loops can help you to perform one task as long as you want, it can be for 5 times or for 500 times or more. Loops are very efficient in this.

There are two different types of loop available in Swift.

1.   While Loop
2.   For Loop

### 1.  While Loop

While loop is useful when you want to perform one task as long as condition is true. Once the condition is false loop will be stopped. What does it mean, lets discuss in detail.

As you know that if statement works with condition. If myvalue is less than 10 then it will print myvalue on your screen.

```var myvalue = 0;

if(myvalue <= 10){
println(myvalue)
}```

While loop also works in same fashion but with repetition. You can do some minor changes in previous code. Repalce if with while, and your while loop is ready for work.

```var myvalue = 0;

while (myvalue <= 10){
println(myvalue)
}```

While loop will check if myvalue is less than 10, it will go inside the loop and print myvalue on the screen, it will go back at the start of while loop and see if myvalue is still less than 10 if yes, it will go inside the loop again and it will print myvalue on the screen. While loop will keep printing as long as myvalue is less than 10.

In this condition you can see that myvalue will be less than 10 all the time. If you want to stop this loop at some point, start incrementing with 1 on every iteration, so the loop will stop when myvalue will exceed more than 10.

```var myvalue = 0;

while (myvalue <= 10){
println(myvalue)
myvalue++
}```

Now the While loop will print numbers from 0 to 10. You can see 11 iterations in output window. To see the complete list in Console output, open View -> Assistant Editor -> Show Assistant Editor

### 2.  For Loop

There is another better and clean way to write a loop, which is called for loop. In for loop, you can initialize the variable, set the condition and increment in this variable. All 3 steps can be done in one line. Here is a syntax

```for(var myvalue = 0; myvalue <= 10; myvalue++){
println(myvalue)
}```

It will produce the same result as while loop but its very easy to understand.

For loop starts from initialization of variable, in your case var myvalue = 0. Then it checks the condition if myvalue is less than or equal to 10, if yes, it goes inside the loop and print myvalue on screen. At the end it increments in myvalue with 1 by using myvalue++.

Now the For loop will print numbers from 0 to 10. You can see the iterations 11 times in output window. To see the complete list in Console output, open View -> Assistant Editor -> Show Assistant Editor

### Rang based For Loop

Swift provides most efficient and convenient way to write for loop, which is called rang based loop. Assign start … end points to your variable and you are done. Swift will take care of everything

```for i in 0...10 {
println("Rang Based Loop \(i)")
}```

yes it is very easy. You can change the starting and ending point as per your requirement.

```var count = 0

for i in 55...74 {
count = i + count
}

println("The count is \(count)")```

Click on quick look and see the graph of your count variable.

### Rang based for loop with strings

Swift also provides rang based loop for strings.  First check the traditional for loop.

```var name = "Swift-Language"

for (var i=0; i < name.lengthOfBytesUsingEncoding(NSUTF8StringEncoding); i++) {
println(i)
}```

Now see how swift makes programmer’s life comfortable.

```var name = "Swift-Language"

for i in name{
println(i)
}```