Using the Swift For-In Loop to Count in Increments

If you’re familiar with a language like C, you may already know how to create a loop that counts. In Objective-C, such a counting loop might use the for loop like this:

for (int i = 1; i <= 10; i++)
{
    // code here
}

T

he typical for loop consists of three parts. First, you need to specify a starting number. Second, you need to specify the ending number. Third, you have to define how to increment the counting number. Typically you would increment by 1 by using the ++ operator.

If you look at this code carefully, you can see plenty of redundancy. In the above Objective-C for loop example, you define a counting variable (i) three times. Since most for loops increment by 1, it’s also cumbersome to keep using the ++ operator all the time. That’s why Swift simplifies the typical for loop by using a for-in loop instead such as:

for i in 0...10  {
   // code here
}

Notice that the Swift for-in loop simply defines a counting variable once and the starting and ending points in a simpler form. That means less code to type and less possibility of making minor syntax mistakes that even professionals make all the time.

However, the Swift for-in loop does lack one feature. By default, the for-in loop increments by 1 so you don’t need to use the ++ operator. Yet what made the typical for loop versatile in C was that you could increment by other numbers besides 1 such as counting by 3 or 5 like this:

for (int i = 1; i <= 10; i = i + 5)
{
    // code here
}

In Swift, you can also count by increments but it’s not as quite straightforward as you might think. To count by increments other than 1, you have to use the where keyword along with the modulus (%) operator such as:

for i in 0...10 where i%3 == 0  {
    print (i)
}

This Swift code says count from 0 to 10 but only where the counting variable (i) divided by 3 equals 0. That essentially means to count in increments of 3. Replace the number 3 by whatever counting increment you want and you can now make the Swift for-in loop count by increments other than 1.

Although Swift simplifies coding over C, this clumsy for-in where loop is one example where Swift actually makes counting in different increments less intuitive.

A far more intuitive but wordier solution is to use the while loop to increment by values other than 1 such as:

var i = 0
while i <= 10 {
    print (i)
    i = i + 3
}

This while loop requires initializing a value first, then specifying an ending point, then incrementing the counting variable inside the while loop itself. If you fail to increment the counting variable inside the while loop, you risk creating an endless loop.

Although this mistake is easy to fix, it’s also easy to make, which means the while loop is a clumsier alternative┬áto the for-in loop.

Now that you know how to count in increments other than 1, you can use this solution easily in your own Swift projects.

November 1st, 2016 by
HTML Snippets Powered By : XYZScripts.com