EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Swift For Loop

Home » Software Development » Software Development Tutorials » Swift Tutorial » Swift For Loop

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.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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)")
}

Popular Course in this category
Software Testing Training (9 Courses, 2 Projects)9 Online Courses | 2 Hands-on Projects | 60+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (4,593 ratings)
Course Price

View Course

Related Courses
Selenium Automation Testing Training (9 Courses, 4+ Projects, 4 Quizzes)Appium Training (2 Courses)JMeter Testing Training (3 Courses)Swift Training (10 Courses, 11+ Projects)Windows Forms Training (4 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)

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

This is a guide to Swift For Loop. Here we discuss the definition, Flowchart, How for loop works in Swift? and examples with code implementation. You may also have a look at the following articles to learn more –

  1. Swift Version
  2. What is Swift?
  3. Swift Operators
  4. How To Install Swift?

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Swift Tutorial
  • Basics
    • How To Install Swift?
    • Swift Version
    • Swift Operators
    • Swift For Loop
    • Swift Dictionary
    • Swift forms
    • Swift map
    • Swift Interview Questions

Related Courses

Swift Training Course

Windows Forms Training Course

Kali Linux Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • 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

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

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

EDUCBA

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

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

Special Offer - Software Testing Training Learn More