Updated April 12, 2023
Definition of Swift For Loop
For loop in Swift is a concept of programming language which is used for iterating any code and to make any code more enhanced and expressive. At times when a situation is redundant and where there is a need to iterate over the list of arrays or dictionaries then swift for loop comes into existence. Sometimes for loop is also used for counting any range of numbers, characters in a string, and many other sequences that are needed for iteration or to get the values for manipulation over the collection and any other data structure which requires calculation.
Syntax:
There is a syntax flow for every loop and programming language So do Swift for loop. Looping in swift is an iteration which is used in a program to repeat a specific block.
for no-of-components in no-of-components
{
perform action
}
Flowchart
Representation of the flow chart for-in loop statement diagram to represent the functionality that will work in swift programming language whenever the need is to do an iteration.
Let’s iterate through the flowchart to reach the end of the list iteration. Start with the iteration for example by counting any component or element present within the list followed by certain conditions that need to be checked in order to reach a conclusion in terms of iteration. Thus if the condition gets satisfied and the condition check with proper codebase exists then it will reach to code block otherwise if it does not exist then it will come out of the conditional loop and will throw just errors and exceptions at the time of execution due to condition failure.
How for loop works in Swift?
- There are many ways in which for loop in Swift works for iterating using for loop in swift, collections For-In to iterate over loops, Arrays, and dictionaries over with specific range in swift.
- It plays a total contradictory role when used with while and do while or other loops in Swift.
- For-In loop in Swift comes as a savior whenever the need is to make any repetitive processes for some fixed amount of time and uses for running any specific tasks for a certain number of times.
- The loop iterates over a specific range of elements and then we can access those elements getting returned from the range in value element.
- The sequence present in the list gets iterated over a range and then that value is set for the first number in that range having other statements in the loop that gets executed.
- Once that statement gets executed and the condition gets satisfied as per requirement then the flow is simple as it will be used for updating the second value within the range and can be used for executing it again.
- This looping process continues till the range of the list and iteration reaches end of any loop and then the loop gets stopped once reached.
- For loop in case of just iteration is used for counting and accessing the elements present within the array or list.
- The stride method is useful when the requirement for using for-in loop is with some periodic interval then in that case it will be used for a specific time to wait and iterate accordingly.
- Accessing over arrays and collections are also possible by using the indexing method or twist with the enumeration inbuild function being described further with the examples.
- All the examples mentioned with for loop comes into use according to scenario and requirement in swift with version compatibility also.
Examples
Let us discuss examples of Swift For Loop.
Example #1
This program represents the for loop for the sequence to get iterated over the list 5 times and which will set the first element into the range and then will store the second number and will iterate simultaneously as shown in the output.
import Foundation
import Glibc
for itrn in 1...5
{
print("Welcome_Everyone!. Value becomes \(itrn)")
}
Output:
Example #2
This program demonstrates the variety of fruits to access all the fruits in the fruit variety i.e. for accessing elements using for-in loop as shown in the output. Also, the sequence that is used for iteration is over an array of strings.
import Foundation
import Glibc
let fruits_Variety = ["Kiwi", "pomegranade", "orange", "apple", "banana", "pineapple"]
for fruits in fruits_Variety
{
print(fruits)
}
Output:
Example #3
This program is used for discarding the values present in a definite range to neglect all the values used for discarding an underscore is appended after the for and before in which symbolizes the change of discard of particular element from the array as shown in the output.
import Foundation
import Glibc
for _ in 1..<5
{
print("Welcome_Everyone..!")
}
Output:
Example #4
This program is used for demonstrating the access of elements of a string collection used for-in loop as shown in the output.
import Foundation
import Glibc
for p_val in "I@mafreesoul..!"
{
print(p_val)
}
Output:
Example #5
This program is used for demonstrating the for-in loop in a scenario where the loop for any fixed value in iteration is used for making each value in the range to use stride method as shown in the output.
import Foundation
import Glibc
let periodic_inttrvl = 8
for i_time in stride(from: 1, to: 20, by: periodic_inttrvl)
{
print(i_time)
}
Output:
Example #6
This program demonstrates the use of a variety of fruits that needs to be iterated when indexing is performed on the list for accessing the element and gives user the ability to search with proper elements easily as shown in the output.
import Foundation
import Glibc
let fruit_variety = ["Apple", "Guava", "Kiwi", "Orange", "banana", "Pineaaple"]
for (indx, variety) in fruit_variety.enumerated()
{
print("\(indx):\(variety)")
}
Output:
Conclusion
Swift for loop is used for mainly iterating the number of elements within the array or collection of lists and is then used for maintaining order. In swift for-in loop it is quite easy to access and traverse till the end of the range, but range of elements do vary easily.
Recommended Articles
We hope that this EDUCBA information on “Swift For Loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.