For Loop in Swift

For Loop in Swift

In Swift, loops are used to repeat a block of code. A for-in loop is a way to do something repeatedly but in a smarter, more organized way.

·

3 min read

When you're stepping into the world of Swift programming, one of the first things you'll encounter is the need to repeat actions multiple times. That's where the for-in loop becomes your trusty companion. In this beginner-friendly guide, we'll delve into the for-in loop in Swift 5, covering its syntax, applications, and some nifty variations to make your journey smoother.

What's a For-In Loop?

In plain English, a for-in loop is a way to do something repeatedly, but in a smarter, more organized way. It's like telling Swift, "Hey, do this for all these things in my list." It's incredibly useful when you want to go through a list of things (like numbers, words, or other items) and do something with each one.

The structure of a for-in loop is straightforward:

for item in list {
    // Do something with 'item'
}
  • item is a placeholder for each element in your list.

  • list is what you want to go through.

Basics of For-In

Let's break it down with a simple example. Imagine you have a list of programming languages:

let languages = ["Swift", "Java", "Go", "JavaScript"]

You want to say something about each language in your list. Here's how you'd do it using a for-in loop:

for language in languages {
    print("I love \(language)!")
}

As you can see, the loop takes each 'language' in the 'languages' list, and the code inside the loop gets executed. In this case, it's printing a message for each language.

Filtering with Where Clause

Sometimes, you want to be a bit picky and do something only if a condition is met. Swift makes it easy with a 'where' clause in your for-in loop. It acts like a filter, allowing you to execute code based on a condition:

for language in languages where language != "Java" {
    print(language)
}

Here, it goes through each 'language' in the 'languages' list but only prints if the language is not "Java." It's a way to filter your list.

Working with Ranges

Ranges are like ordered sequences of numbers. They come in handy when you want to repeat something a specific number of times. You can define a range like this:

let numbers = 1...5

This creates a range from 1 to 5, including both 1 and 5. Now, you can use a for-in loop to do something with these numbers:

for number in numbers {
    print(number)
}

In this example, you go through the range from 1 to 5, and the 'number' variable takes on each value in the range. The code inside the loop gets executed for each number.

Stepping through with 'stride' Function

What if you don't want to go through a continuous range but instead skip some numbers? That's where the 'stride' function shines. It helps you create a sequence with defined intervals:

for i in stride(from: 1, to: 10, by: 2) {
    print(i)
}

Here, you go from 1 to 10, but you skip every other number with an interval of 2. So, it prints 1, 3, 5, 7, and 9.

Conclusion

You've just scratched the surface of what for-in loops can do in Swift. These loops are like your magic wand for repeating actions, filtering, and stepping through sequences. As you continue your Swift journey, you'll discover even more exciting ways to use them, helping you tackle various programming challenges. Happy coding!

More Swift Tutorials

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!