EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Software Development Basics Swift For Loop

Swift For Loop

Anusua Dutta
Article byAnusua Dutta
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated April 12, 2023

Swift For Loop

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

 Swift For Loop

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:

Swift For Loop 1

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:

Swift For Loop 2

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:

Swift For Loop 3

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:

Swift For Loop 4

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 4

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:

example 6

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.

  1. Swift Version
  2. What is Swift?
  3. Swift Operators
  4. How To Install Swift?
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SWIFT Course Bundle - 21 Courses in 1
 56+ Hours of HD Videos
21 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more