Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
For loop in Swift is a concept of programming language which is use 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 use for counting any range of numbers, characters in a string,
and many other sequences that are need 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 also 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 check in order to reach a conclusion in terms of iteration. Thus if the condition gets satisfy 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?
Examples
Let us also discuss examples of Swift For Loop.
Example #1
This program represents the for loop for the sequence to get iterate 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)”)
}
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 use 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)
}
Example #3
This program is also use for discarding the values present in a definite range to neglect all the values use for discarding an underscore is append 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..!”)
}
Example #4
This program is also use 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 also 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.
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.