DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

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)