DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

1 1

Day 4 : 100DaysOfSwift🚀

Day 4

Loops

the ability to repeat some simple task billions of times every second is called Loops.

**For loop

let count = 1...10
for number in count {
    print("Number is \(number)")
}
Enter fullscreen mode Exit fullscreen mode

Ans:

Number is 1
Number is 2
Number is 3
Number is 4
....
Number is 20
Enter fullscreen mode Exit fullscreen mode

With underscore(_)

for _ in 1...5 {
    print("Saurabh")
}
Enter fullscreen mode Exit fullscreen mode

Ans:

Saurabh
Saurabh
Saurabh
Saurabh
Saurabh
Enter fullscreen mode Exit fullscreen mode

While Loop
A second way of writing loops is using while: give it a condition to check, and its loop code will go around and around until the condition fails.

var number = 1

while number <= 10 {
    print(number)
    number += 1
}

Enter fullscreen mode Exit fullscreen mode

Ans:

1
2
3
4
5
..
10
Enter fullscreen mode Exit fullscreen mode

Summary
1.Loops let us repeat code until a condition is false.
2.If you don’t need the temporary constant that for loops give you, use an underscore instead so Swift can skip that work.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more