Introduction
This is part two of the Swift Programming - Zero to Hero series. In this article we will see loops in Swift. Please follow my preious article before proceeding further.
Loop in Swift
Loops are used to iterate data from an array, range of numbers, or character from a string. Consider a situation in your program where you have to execute the same set of code ten times. In such a situation, instead of writing the same code ten times, define the code in a Loop and execute it ten times. This saves the complexity and time involved in coding. The following are the Looping statements mainly used in Swift.
- for
- for-in
- while
- do...while
For loop
This is the traditional method of looping that executes a sequence of statements multiple times and abbreviates the code that manages the Loop variables.The syntax of For loop consists of initializer, condition, and loop expression. Initializer initializes a variable; the Loop expression increments or decrements the value of the variable; and the condition refers to a condition specified in the loop.
Syntax
- for (init;condition;loop expression){
- statements
- }
The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
The condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to the loop expression statement. This statement allows you to update any loop control variables.
For -in loopThe for-in loop iterates over collections of items, such as ranges of numbers, items in an array, or characters in a string. The working of for-in loop is similar to for loop. The statements in for-in loop continue to execute for each element in a collection. After iterating each elements in collection, the control transfers to next block of code.
Syntax
- for index in var {
- statement(s)
- }
While
We can use while loop to execute a statement until specified condition evaluates to false. The best scenario to use while loop is when we don’t know how many times the loop should be executed. If the Condition in While loop is true, then the statements inside while loop are executed. Otherwise the statements are not executed.
Syntax
- while condition {
- statement(s)
- }
do… while
The function of do…while loop is similar to the while loop. The main difference in do..while loop is that the condition is checked at the end of the loop. This means the loop executes at least once even if the condition is false.
Syntax
- do {
- statement(s);
- }while( condition );
Conclusion
In this article we learned about looping statements, I hope it was helpful. In my next article, we will see Arrays and accessing Array elements using Looping Statements and Conditional Statement.

Gayathri AnbazhaganPosted Aug 9, 2017, 8:41 PM
Nice brother....good start....Thanks for sharing....bro
Vidyadharran GPosted Aug 8, 2017, 10:27 PM
Nice bro thanks for sharing