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)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More